<?php
/*
Plugin Name: Mike bike tours booking plugin
Plugin URI: http://www.ficklefish.nl
Description: Reservation plugin customised for Mike bike tours, just use {BOOKING} in your page and your booking form will be put in your page.
Author: Abu Saebu/Esther Verboven
Author URI: http://www.ficklefish.nl
*/
/* Copyright2012 Abu Saebu

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is gdistributed in the hoe that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more deails.

	You shoud have recived a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Flor, Boston 2110-1301  USA

*/

session_start();
foreach ( $_REQUEST as $key => $value ) {
		$_REQUEST[$key] = filter_var($value, FILTER_SANITIZE_STRING);
}
		
define ("RES_PLUGIN_DIR", basename(dirname(__FILE__)));
define ("RES_PLUGIN_URL", get_settings("siteurl")."/wp-content/plugins/".RES_PLUGIN_DIR);
define ("RES_PLUGIN_PATH",ABSPATH."wp-content/plugins/".RES_PLUGIN_DIR);
define ("RES_CUR_PAGE","http://".$_SERVER["HTTP_HOST"].$_SERVER["REDIRECT_URL"]);
add_action("init", "res_init");
add_action("admin_init", "res_adm_init");
add_action("admin_menu", "res_menu");
add_action("admin_head", "res_add_head" );
add_action("wp_head", "res_add_head" );
add_filter("the_content","res_insert");
add_filter("plugin_action_links", "res_links", 10, 2 );
register_activation_hook(__FILE__,'activate');

$path_to_php_file_plugin = "mb-reservation/mb-reservation.php";
add_action("deactivate_" . $path_to_php_file_plugin, "deactivate"); 
add_action("activate_" . $path_to_php_file_plugin,  "activate"); 

require_once(RES_PLUGIN_PATH. "/mb-res-pro.php" );  
$GLOBALS["version"] = "pro";
add_action("show_user_profile", "show_user_fields");
add_action("edit_user_profile", "show_user_fields");
add_action("personal_options_update", "update_user_fields");
add_action("edit_user_profile_update", "update_user_fields");

function res_adm_init() {
	if ($_REQUEST['page']=="resources") {
		wp_enqueue_script('jquery');
		wp_enqueue_script('jquery-form');
		wp_enqueue_script('jqtreetable', RES_PLUGIN_URL.'/js/jQTreeTable/jqtreetable.js');
		wp_enqueue_script('jquery-ui-dialog');
		wp_enqueue_script('jquery-ui-resizable'); 
	}	
}


function resLocale($locale = "") { 
	global $locale;
	
	$mofile = RES_PLUGIN_PATH  .'/lang/'.RES_PLUGIN_DIR.'-'.$locale.'.mo';
	return load_textdomain(RES_PLUGIN_DIR, $mofile);
	
	if ( empty( $locale ) ) $locale = get_locale();
	if ( !empty( $locale ) ) {		
		$mofile = RES_PLUGIN_PATH  .'/lang/'.RES_PLUGIN_DIR.'-'.$locale.'.mo';
			
		if (file_exists($mofile))    
			return load_textdomain(RES_PLUGIN_DIR, $mofile);
		else                        
			return false;
	} 
	return false;
}

if ( !function_exists('wp_sanitize_redirect') ) :
	function wp_sanitize_redirect($location) {
		$location = preg_replace('|^a-z0-9-~+_.?#=&;,/:%!|i', '', $location);
		$location = wp_kses_no_null($location);
		
		$strip = array('%0d', '%0a', '%0D', '%0A');
		$location = _deep_replace($strip, $location);
		return $location;
	}
endif;

function res_links($links, $file){ 
	
	static $this_plugin;
	if (!$this_plugin) $this_plugin = plugin_basename(dirname(__FILE__).'/mb-reservation.php');
	
	if ($file == $this_plugin){
		$settings_link1 = '<a href="admin.php?page=settings">' . __('Settings', 'mb-reservation') . '</a>';
		$settings_link2 = '';
		array_unshift( $links, $settings_link1 ); 
	}
	return $links;
}

function res_init()
{

	wp_register_script('jscal2', RES_PLUGIN_URL	   .'/js/jscal2/jscal2.js');
	wp_enqueue_script('jscal2');
	$lang='en';

	if (file_exists(RES_PLUGIN_PATH.'/js/jscal2/lang/'.substr(get_locale(),0,2).'.js')) { $lang=substr(get_locale(),0,2);}
	wp_register_script('jscal2lang', RES_PLUGIN_URL.'/js/jscal2/lang/'.$lang.'.js');
	wp_enqueue_script('jscal2lang');
	
	wp_enqueue_script('jquery');
	wp_enqueue_script('jquery-ui-dialog');
	
}



function res_add_head()
{
resLocale() ;

if ($_REQUEST['pager']=="selectajax") {select_ajax();  exit;}
if ($_REQUEST['pager']=="sberprint") {echo sberprint();  exit;}


echo '
<link rel="stylesheet" type="text/css" href="'.RES_PLUGIN_URL.'/css/calendar/jscal2.css" />
<link rel="stylesheet" type="text/css" href="'.RES_PLUGIN_URL.'/css/calendar/border-radius.css" />
<link rel="stylesheet" type="text/css" href="'.RES_PLUGIN_URL.'/css/calendar/'.get_option("res_calendar_color").'/'.get_option("res_calendar_color").'.css" />
<link rel="stylesheet" type="text/css" href="'.RES_PLUGIN_URL.'/css/res/'.get_option("res_color").'/style.css" />
<link href="'.RES_PLUGIN_URL.'/css/res/res.css" rel="stylesheet" type="text/css" /> 
';
	
if (is_admin()) { echo '<link href="'.RES_PLUGIN_URL.'/css/res/admin.css" rel="stylesheet" type="text/css" /> ';}
}


function res_insert($content)
{ 


	if (preg_match('{BOOKING([0-9]*)}',$content,$interf))
	{
		$interf=$interf[1];
		$res_output = res_page(0,$interf);
		$content = preg_replace('/{BOOKING([0-9]*)}/',$res_output,$content);
	}

	$content=pro_res_insert($content); 

	return $content;
}


function res_page($charr,$interf)
{
	//print_r($_REQUEST);
	$content = "";	
	$content .= '<div class="reservation">';
	switch ($_REQUEST['pager']) {
	/* validation step 1 */
	case '1':  
		$_SESSION['mb-booking'] = array( 'booktype' => $_REQUEST['booktype'],
								  'grownups' => intval($_REQUEST['grownups']),
								  'students' => intval($_REQUEST['students']),
								  'children' => intval($_REQUEST['children']),
								  'time' => $_REQUEST['time'],
								  'email' => $_REQUEST['email'],
								   'surname' => $_REQUEST['surname'],
								    'address' => $_REQUEST['address'],
									'phone' => $_REQUEST['phone']
								  );
		if (strtotime($_REQUEST['dateend'])<=strtotime($_REQUEST['datebegin']) && $interf==4)
		{
				$err .="<br>". __(" - Arrival date more then Departure date","mb-reservation");
				if ($err!="") {
						$err = "<div class=alert>".__("When completing the form, the following error(s) occurred:","mb-reservation") . $err."</div>";
						$content .=($err);
						$content .= pageI4($interf);
						
				}
		}
		else
		{		
			if (empty($_REQUEST['datebegin']) || empty($_REQUEST['dateend'])) {
				$err="";
				if ($_REQUEST['datebegin']=="") 
				{
								$err .= "<br>".__(" - Not specified Arrival date","mb-reservation");
				}
				if ($_REQUEST['dateend']=="") 
				{
								$err .="<br>". __(" - Not specified Departure date","mb-reservation");
				}
				if ($err!="") 
				{
								$err = "<div class=alert>".__("When completing the form, the following error(s) occurred:","mb-reservation") . $err."</div>";
								$content .=($err);
								$content .= pageI4($interf);				
				}
			}
			elseif (!isset($_REQUEST['terms']) || !isset($_REQUEST['booktype'])) 
			{
					$err="";
					if (!isset($_REQUEST['terms']))
					{			
						$err .="<p class='error'>". __(" - You should agree with our terms and conditions.","mb-reservation")."</p>";	
					}
					if (!isset($_REQUEST['booktype']))
					{	
						$err .="<p class='error'>". __(" - You didn't choose a tour or bike.","mb-reservation")."</p>";	
					}
					if ($err!="") 
					{					
						$content .=($err);
						$content .= page0($interf);		
					}
			}
			elseif ( (intval($_REQUEST['grownups']) + intval($_REQUEST['students']) + intval($_REQUEST['children'])) == 0 ) 
			{
						$err .="<p class='error'>". __(" - You didn't fill in the number of bikes.","mb-reservation")."</p>";	
						$content .=($err);
						$content .= page0($interf);		
			}
			elseif ( (intval($_REQUEST['kolday'])) == 0 ) 
			{
						$err .="<p class='error'>". __(" - You didn't fill in the number of bikes.","mb-reservation")."</p>";	
						$content .=($err);
						$content .= page0($interf);		
			}

			else  
			{
				$_SESSION['email'] = $_REQUEST['email']; 
				require_once ( ABSPATH . WPINC . '/registration.php' );		
				$user_info = email_exists($_REQUEST['email']);
				/* user handling */
				if ($user_info)	
				{

					$user_info=get_userdata($user_info);	
					//print_r ($user_info);
					$_SESSION['userid'] = $user_info->ID;
					
					/*$content .= '<form action="'. get_option("home").'/wp-login.php" method="post">
					<input type="hidden" name="log" id="log" value="'. wp_specialchars(stripslashes($user_info->user_login), 1) .'" size="20" />
					<input type="hidden" name="pwd" value="mb-secret-password" id="pwd" size="20" />
					<input name="rememberme" id="rememberme" type="hidden"  value="forever" /> 	
					<input type="hidden" name="redirect_to" value="'. $_SERVER['REQUEST_URI'] .'</form>';	*/
				}	
				else 
				{ 
					$userid = wp_insert_user (array(
						'user_login'	=> $_REQUEST['email'],
						'user_nicename'	=> $_REQUEST['email'],
						'user_email'	=> $_REQUEST['email'],
						'user_url'		=> 'http://',
						'display_name'	=> $_REQUEST['email'],
						'user_pass' =>      'mb-secret-password',
						'first_name' =>  $_REQUEST['surname'],
						'last_name' =>  $_REQUEST['surname']
						));
						

					$_SESSION['userid'] = $userid ; 
					
					
					update_user_fields($userid);	// saving of address and phonenumber		
														
				}
				/* end user handling */
				$content .= page1(array("+",$_REQUEST['booktype']));
				

			}
		}
	break ; 
	case '2': if (array_sum($_REQUEST['reskol'])==0) {
					$content .= "<div class=alert>".__("You must select at least one of the resources","mb-reservation")."</div>"; $content .= page1($charr);
				} 
			  else {
					$content .= page2a();      
			  }
	break ; 
	case '3':  {		
		$content .= page3();	
		break ; 	
	}	
	case '2b': { 	$err="";
			
			if ($_REQUEST['log']=="") {
				$err .= __(" - Not specified (email address)","mb-reservation");
			}
			
			if ($err!="") {
				$err = "<div class=alert>".__("When completing the form, the following error(s) occurred:","mb-reservation") . $err."</div>";
				$content .=($err); $content .= page2a();
			} else {
				if (!eregi("^[._a-zA-Z0-9-]+@[.a-zA-Z0-9-]+.[a-z]{2,6}$", $_REQUEST['log'])) {$err.= __("That isn`t a valid e-mail address.  E-mail addresses look like: username@example.com","mb-reservation");
					$err = "<div class=alert>".$err."</div>"; $content .= $err; $content .= page2a();
				} else {
					$content .= page2b();}
			} }         break ; 
		
	case 'mobcash': $content .= mobcash();        break ; 
	//case 'selectajax':  select_ajax();  exit;       break ; 
	case 'sber': $content .= sber();        break ; 
	case 'sberprint': $content .= sberprint();        break ; 
	case 'webmoney': $content .= webmoney ();        break ; 
	case 'robokassa': $content .= robokassa ();        break ; 
	case 'paypal': $content .= paypal ();        break ; 
	case 'confirm_paypal': $content .=confirm_paypal(); break;  
	case 'success_paypal': $content .=success_paypal(); break; 
	case 'cancel_paypal': $content .=cancel_paypal(); break; 
	case 'nopay': $content .= "<div class=alert>".__("You have not chosen a payment system","mb-reservation")."</div>"; $content .= page3 ();break ; 
		
	default: 
		if ($interf!=2 && $interf!=3 && $interf!=4 )  	
			$content .= page0(); 
		else 
			if ($interf==4) $content .= pageI4($interf); 
			else $content .= pageI23($interf);               
	break;
	}

	$content .= '</div>';

	return $content;

}


function update_table_ajax() {
	global $wpdb;
	if($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
		$time = date("H:i:s", time());
		header('Content-Type: text/html; charset=utf-8');
		//	print_r ($_REQUEST); exit;

		if (isset($_REQUEST['resourceid'])) {

			$qry="update ".$wpdb->prefix ."res_resources  set ";

			foreach ($_REQUEST as $key=>$value) {
				if ($key!="page" && !strpos($key,"id")) {
				if (($value)=='')  $value="NULL";
				$qry.=	" $key='$value' ,";

				}
				if (strpos($key,"id")) $key_id=$key;	
			}
			$qry=substr($qry,0,-1).	" where $key_id='".$_REQUEST[$key_id]."' ";
									
			$wpdb->query($wpdb->prepare($qry) );

			echo '<img src="'.RES_PLUGIN_URL.'/js/jQTreeTable/images/load.gif">';
			exit 	;


		}
	}	
}



function select_ajax() {
	global $wpdb;
	//if($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') 
	{
		$time = date("H:i:s", time());
//		header('Content-Type: text/html; charset=utf-8');
		//	print_r ($_REQUEST); exit;

	list($results,$map,$treet,$level)=treesort();
	
	
	$treeselect=treeselect($results,$level,42);

			echo '<br><b>'.__("Room","mb-reservation").'</b><br>'.$treeselect;
			
			//.'<img src="'.RES_PLUGIN_URL.'/js/jQTreeTable/images/load.gif">';
	//		exit 	;


		
	}	
}



function res_menu() {

global $submenu, $menu	, $wpdb;


if (isset($_REQUEST["expand_x"]) || $_REQUEST['action']=="add") setcookie ("treet", "", time() - 3600);             
if (isset($_REQUEST["collapse_x"])) {  

list($results,$map,$treet)=treesort(); 

setrawcookie ("treet", $treet);
}

resLocale() ;

if (true) { $user_role_plugin = get_option("res_security_plugin") ; $user_role_settings = get_option("res_security_settings") ;}

update_table_ajax();



	add_menu_page(__("Reservation","mb-reservation"), __("Reservation","mb-reservation"), $user_role_plugin,"ordertable" , 'res_options'
,RES_PLUGIN_URL."/img/ico16x16.png");
	add_submenu_page("ordertable", __("Reservation","mb-reservation"), __("Orders","mb-reservation"), $user_role_plugin,"orders", 'res_options');
	add_submenu_page("ordertable", __("Reservation","mb-reservation"), __("Reserve","mb-reservation"), $user_role_plugin,"makeorder", 'res_options');
	add_submenu_page("ordertable", __("Reservation","mb-reservation"), __("Resources","mb-reservation"), $user_role_plugin, "resources", 'res_options');
	//add_submenu_page("ordertable", __("Reservation","mb-reservation"), __("Special offers","mb-reservation"), $user_role_plugin, "offers", 'res_options');
	add_submenu_page("ordertable", __("Reservation","mb-reservation"), __("Settings","mb-reservation"), $user_role_settings, "settings", 'res_options');
	add_submenu_page("ordertable", __("Reservation","mb-reservation"), __("Add availability","mb-reservation"), $user_role_settings, "add-availability", 'res_options');

	if ($GLOBALS["version"]	!= "pro") add_submenu_page("orders", __("Reservation","mb-reservation"), __("Pro","mb-reservation"), $user_role_settings, "pro", 'res_options');
	$submenu[plugin_basename( "ordertable" )][0][0] = __("Orders table","mb-reservation");	
    

	
	}




function res_options() {

echo '<div class="wrap">';
echo menu_admin();			
	if(isset($_REQUEST['page']))  switch ($_REQUEST['page']) {
		
		case "resources" : 	adm_resources(); break;
		case "orders" : 	
			if ($_REQUEST['subpage']=="mail")  {adm_mailbron();	break;}
			elseif ($_REQUEST['subpage']=="suborder")  {adm_suborder();	break;}
			else {adm_orders(); break;}
		case "mail" : 		adm_mailbron(); break;
		case "suborder" : 	adm_suborder(); break;
		case "offers" : 	adm_offers(); break;
		case "settings"  :  adm_settings(); break;
		case "add-availability"  :   adm_availability(); break;
		case "pro"  :  		adm_pro(); break;
		case "makeorder" : 		adm_makeorder0(); break;
		case "makeorder1" : 	adm_makeorder1();	 break;
		case "makeorder2" : 	adm_makeorder2();	 break;
		case "ordertable" : echo adm_ordertable(); break;
	
	}
	else  
		adm_orders();
						
echo '</div>';			
			
	
	
}

function activate() { 
	global $wpdb;
	$table_name = $wpdb->prefix . "res_resources";	

	if($wpdb->get_var("show tables like '$table_name'") != $table_name ) {
	$sql = "CREATE TABLE $table_name (
		`resourceid` MEDIUMINT(9) NOT NULL AUTO_INCREMENT,
		`parent` MEDIUMINT(9) NOT NULL  DEFAULT '0' ,
		`name` TINYTEXT,
		`price` DECIMAL(11,2) DEFAULT NULL,
		`price1` DECIMAL(11,2) DEFAULT NULL,
		`price2` DECIMAL(11,2) DEFAULT NULL,
		`capacity` INTEGER(11) DEFAULT NULL,
		`calcType` enum('0','1') NOT NULL DEFAULT '0',
		PRIMARY KEY (`resourceid`)
		)";

		require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
		dbDelta($sql);
	
	}
	$table_name = $wpdb->prefix . "res_orders";
	if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
		$sql = "CREATE TABLE $table_name (
		`orderid` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
		`userid` BIGINT(20) UNSIGNED NOT NULL,
		`price` DECIMAL(11,2) DEFAULT NULL,
		`datebegin` DATE DEFAULT NULL,
		`dateend` DATE DEFAULT NULL,
		`comments` TEXT ,
		`payed` TINYINT(4) DEFAULT NULL,
		`paysys` MEDIUMINT(9) DEFAULT NULL,
		PRIMARY KEY (`orderid`),
		KEY `userid` (`userid`)
		)";
		require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
		dbDelta($sql);		
	}
	
	$table_name = $wpdb->prefix . "res_orders_content";
	if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
		$sql = "CREATE TABLE $table_name (
		`id` MEDIUMINT(9) UNSIGNED NOT NULL AUTO_INCREMENT,
		`orderid` BIGINT(20) UNSIGNED NOT NULL,
		`resourceid` MEDIUMINT(9) DEFAULT NULL,
		`kol` INTEGER(11) DEFAULT NULL,
		`price` DECIMAL(11,2) DEFAULT NULL,
		PRIMARY KEY (`id`),
		KEY `userid` (`orderid`)
		)";
		require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
		dbDelta($sql);				
	}
	$table_name = $wpdb->prefix . "res_paysys";
	if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
		$sql = "CREATE TABLE $table_name (
		`paysysid` MEDIUMINT(9) NOT NULL AUTO_INCREMENT,
		`name` TINYTEXT,
		PRIMARY KEY (`paysysid`)
		)";
		require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
		dbDelta($sql);	
		
		$sql = "	INSERT INTO $table_name (`paysysid`, `name`) VALUES
		(1,'Qiwi'),
		(2,'PayOnlineSystem'),
		(3,'Sberbank'),
		(4,'Robokassa'),
		(5,'Webmoney'),
		(6,'PayPal'); ";
		require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
		dbDelta($sql);	
	}
	$table_name = $wpdb->prefix . "res_offers";
	if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
		$sql = "CREATE TABLE $table_name (
			`id` MEDIUMINT(9) NOT NULL AUTO_INCREMENT,
			`resourceid` MEDIUMINT(9) UNSIGNED NOT NULL,
			`datebegin` DATE NOT NULL,
			`dateend` DATE NOT NULL,
			`price` DECIMAL(11,2) NOT NULL,
			`description` TEXT ,
			UNIQUE KEY `id` (`id`)
			)";

		require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
		dbDelta($sql);				
		
	}
	
	$table_name = $wpdb->prefix . "res_resources";	
	
	$sql = "INSERT INTO $table_name (resourceid, parent, name, price, capacity) VALUES
	(136,0,'City Tour',NULL ,NULL ),
	(137,0,'Country Tour',NULL ,NULL ),
	(138,0,'Rent a Bike',NULL ,NULL )";
	
	$wpdb->query($sql);				

	add_option("res_db_version", "0.1");
	add_option("res_days","14");
	add_option("res_color","white");
	add_option("res_calendar_color","gold");
	add_option("res_security_plugin","7");
	add_option("res_security_settings","10");
	add_option("res_full_uninstall","");
	add_option("res_terms","Your terms of booking");
	add_option("res_calc","1");

	pro_add_settings();
}

function adm_availability() { 
	global $wpdb;
	$table_name = $wpdb->prefix . "res_resources";
	
	$sql = "ALTER TABLE  $table_name ADD  `days` varchar(7) NOT NULL DEFAULT  'MTWTFSS' AFTER  `calcType`";
	$wpdb->show_errors();
	$wpdb->query($sql);		


	$sql = "ALTER TABLE  $table_name ADD  `available` varchar(8) NOT NULL DEFAULT  '01013112' AFTER  `calcType`";
	 $wpdb->show_errors();
	$wpdb->query($sql);		

	$sql = "ALTER TABLE  $table_name ADD  `offline` varchar(1) NOT NULL DEFAULT  '0' AFTER  `calcType`";
	 $wpdb->show_errors();
	$wpdb->query($sql);		

	$sql = "ALTER TABLE  $table_name ADD  `remarks` varchar(255) DEFAULT  '' AFTER  `calcType`";
	 $wpdb->show_errors();
	$wpdb->query($sql);		
	
	die(  "fields added to ".$table_name);
}

function deactivate() 
{ 
	global $wpdb;
	if (get_option("res_full_uninstall"))
	{

		$sql = "DROP TABLE IF EXISTS ".$wpdb->prefix . "res_offers, ".$wpdb->prefix . "res_orders, ".$wpdb->prefix . "res_orders_content, ".$wpdb->prefix . "res_paysys, ".$wpdb->prefix . "res_resources ";	
		$wpdb->query($sql);					

		delete_option("res_db_version");
		delete_option("res_days");
		delete_option("res_color");
		delete_option("res_calendar_color");
		delete_option("res_security_plugin");
		delete_option("res_security_settings");
		delete_option("res_full_uninstall");
		delete_option("res_terms");
		delete_option("res_calc");
			
		pro_delete_settings();
		
		
	}


}

function res_pass ()
{

	$fp = file ("pass.txt",1);
	return (trim($fp[rand(0,count($fp)-1)]).rand(100,999));

}

function getTours() {

$list ="";
	$today = date("Ymd");  $today2 = date("Y-m-d");
	list($results,$map) = treesort();

	//var_dump($results);
	//var_dump($map);
	$js_availability ="[";
	$delim = ""; 
	foreach( $results as $tour) {
	
	//offline is [10]
	if ($tour[10] == '1') continue;
	
		$id = $tour[0];
		$title = $tour[2];
		
		$js_availability .= $delim."{ 'p' : '".$tour[8]."',";
		$js_availability .= "  'i' : '".$tour[0]."',";
		$js_availability .= "  'd' : '".$tour[9]."' }";
		$list .='<p class="tour"><input class="line" type="radio" name="booktype" value="'.$id .'"><label class="line" >'.$title.'</label></p>';
		
		if (!empty($tour[11]))
			$list .="<strong class='remark'>".$tour[11]."</strong>";
			
		$delim = ",";
	}
	$js_availability .= "]";


return array($list, $js_availability);
}

/* first step in reservation flow */
function page0() {

  if (isset($_SESSION['mb-booking'])) {
		foreach( $_SESSION['mb-booking'] as $key=>$val) {
				$$key = $val;
		}
	}
	else {		
		$grownups = $students = $children = $time= $email = $surname = $phone = "";				
	}
	$today = date("Ymd");  $today2 = date("Y-m-d");

	list($tourList, $jsArray) = getTours();
	$content = "";
	$content .= <<<EOF
	<p>
EOF
.__('Please fill in this reservation form.', 'mb-reservation').
<<<EOF
	</p>
	<style>
		.red {
			color: red;
			font-weight: bold;
		}
		.ooSight {
			position: absolute;
			top: -2000px;
		}
		#qNumdays {
			padding: 0 0 0 1em;
		}
		.remark {
			font-size: 75%;
			display: none;
			padding-left: 2.2em;
		}
		.tour {
			line-height : 1.8em;
			margin: 0;
		}
	</style>

	<form name="res_order"  action="" method=post  onsubmit="return valideer(this);" >
	<input type="hidden" name=pager value="1" >
	
	<fieldset>
	<legend>1. Your details</legend>
	<p class="lbl-input">
	<label>Name *</label>
	<input type="text" name="surname" value="$surname" required>
	</p>
	<p class="lbl-input">
	<label>Email *</label>
	<input type="email" name="email" value="$email" required>
	</p>
	<p class="lbl-input">
	<label>Address in Amsterdam</label>
	<input name="address" type="text" value="$address">
	</p>
	<p class="lbl-input">
	<label>Telephone number</label>
	<input type="text" name="phone" value="$phone">
	</p>
	</fieldset>
	<fieldset>
	<legend>2. Your reservation</legend>
	<p class="radio-input">
	<label>Choose a tour or bike *</label>
	$tourList
	</p>
	<p class="lbl-small-input">
	<label for="numberbikes">
EOF
.__("How many bikes do you need ?","mb_reservation").
<<<EOF
 *</label>
	<input type="hidden" name="numberbikes" value="1">
	</p>
	<p id="msgmax" class="info ooSight">
Our tours are for maximum 9 people. If you like to book for more people, please <a href='http://www.mikesbiketoursamsterdam.com/mikes-bike-tours-rentals-amsterdam-location/'>contact us</a>.
	</p>
	<p class="lbl-small-input ppl"><label class="line" >Adults #</label><input class="small" type="text" name="grownups" value="0"></p>
	<p class="lbl-small-input ppl"><label class="line" >Students #</label><input class="small" type="text" name="students"  value="0"></p>
	<p class="lbl-small-input ppl"><label class="line" >Children #</label><input class="small" type="text" name="children" value="0"></p>
	<p class="info">(discount for students and children only apply for tours)</p>
	</fieldset>
	<fieldset>
	<legend>3. Choose a date & time</legend>
	<p class="lbl-date-input">
	<label for="datebegin2">Start date</label>
	<input size="10" id="datebegin2" name="datebegin" value="$today2" />
	<input type="hidden" id="dateend" name="dateend" value="$today2" />	
	<button id="f_btn2" onclick="return false;">calendar</button>

	<div id="qNumdays" class='ooSight'>
	<label>How many days ?</label>
	<select name=kolday class="koldays" >
EOF;
	$maxdays=get_option("res_days");
	for($i=1;$i<=$maxdays;$i++) { 
		$content .= "<option value=\"$i\" >$i</option>";
	}	
	
	$content .= <<<EOF
	</select>
	</div>
	<p class="cal-clone">
	</p>
	</p>
		<p class="lbl-input pickup"><label class="line wider">What time would you like to pick up your bike?</label><input type="time" class="time" name="time" value="" required  >
		</p>
	<p class="radio-input">
	<input class="line"  type="radio" name="terms" value="1" required><label  class="line" >I agree to <a href="http://www.mikesbiketoursamsterdam.com/wp-content/uploads/2013/01/Terms-and-condition.pdf" target="_blank">terms and conditions</a></label>
	<label>Your reservation is confirmed only after payment</label>
	</p>

	</fieldset>
	<p class="submit">
	<input class="res-next" type="submit" value="
EOF
.__("Next","mb-reservation").
<<<EOF
">
	</p>
	</form>
	<script type="text/javascript">//<![CDATA[
	var availability = {$jsArray};
	console.log(availability);
	
	function valideer(frm) {
			jQuery('#msgmax').addClass('ooSight');

			var numberbikes = parseInt(jQuery('input[name=grownups]').val()) + parseInt(jQuery('input[name=students]').val()) + parseInt(jQuery('input[name=children]').val()) ;

		if (numberbikes > 9) {
			jQuery('#msgmax').removeClass('ooSight');
			jQuery('#msgmax').css('color','red');
			return false;
		}
		if (true) {
			//alert(selectedDate.getDay());
			
			var checked = null;
			jQuery('input[name="booktype"]').each( function (i) {
					if (jQuery(this).attr('checked') == 'checked')
						checked = i;
			});
			
			bookedDay = selectedDate.getDay() == 0 ? 6 : selectedDate.getDay()-1;   /* 0 is zondag */
			//alert(bookedDay);
			var avDays = availability[checked].d;
			var avP    = availability[checked].p;
			
			var avDateStart = new Date(selectedDate.getFullYear(), avP.slice(2,4)-1, avP.slice(0,2));
			
			var avDateEnd = new Date(selectedDate.getFullYear(), avP.slice(6,8)-1, avP.slice(4,6));
			//console.log(avDateStart);
			//console.log(avDateEnd);

			if (selectedDate > avDateStart && selectedDate < avDateEnd) {
				;
			}
			else {
				alert('The date you want to book is not in the period in which the tour is available');
				return false;
			}
			
			//alert (avDays);
			//alert( avDays.slice(bookedDay,bookedDay + 1) );
			
			if ( availability[checked].d.slice(bookedDay,bookedDay + 1) == 'X' ) {
				alert('On this weekday this tour is not available');
				return false;
			}
			return true;
		}
		
		return true;
	}
	function updTotal() {
		jQuery('#msgmax').addClass('ooSight');
		
		var numberbikes = parseInt(jQuery('input[name=grownups]').val()) + parseInt(jQuery('input[name=students]').val()) + parseInt(jQuery('input[name=children]').val()) ;
		jQuery('input[name=numberbikes]').val(numberbikes);
		
		if (numberbikes > 9) {
			jQuery('#msgmax').removeClass('ooSight');
		}
		else {
			jQuery('#msgmax').addClass('ooSight');
		}
		
	}
	jQuery(document).ready( function() {
	
	
		jQuery('input[name=booktype]').each( function() {
			if ( jQuery(this).val() == '$booktype')
				jQuery(this).attr('checked','checked');
		});
		
		if ('$booktype' == '138') {
				jQuery('.pickup').show();
				jQuery('input[name=time]').attr('required','');
				jQuery('#qNumdays').removeClass('ooSight');
		}
		else {
				jQuery('input[name=time]').removeAttr('required');
				jQuery('.pickup').hide();
				jQuery('#qNumdays').addClass('ooSight');
		}
		
		jQuery('input[name=grownups]').on( 'change',function() {
			updTotal();
		});
		jQuery('input[name=students]').on( 'change',function() {
			updTotal();
		});
		jQuery('input[name=children]').on( 'change',function() {
			updTotal();
		
		});
		
		 var h = jQuery('.DynarchCalendar-topCont').height();  //.clone().appendTo('.cal-clone');
		 jQuery('.cal-clone').height( h + 'px');
		 var o = jQuery('.cal-clone').offset();
		  jQuery('.DynarchCalendar-topCont').css({'top': o.top + 'px', 'left': o.left +'px'});
		
		jQuery('input[name=booktype]').on('change', function() {
				if ( this.value == 138 && this.checked === true ) {
					jQuery('.pickup').show();
					jQuery('input[name=time]').attr('required','');
					jQuery('#qNumdays').removeClass('ooSight');

				}
				else {
					jQuery('input[name=time]').removeAttr('required');
					jQuery('.pickup').hide();
					jQuery('#qNumdays').addClass('ooSight');
					jQuery('select[name="kolday"] option:first').attr('selected',true);
				}
				jQuery('.remark').hide();
				jQuery(this).parent().next().slideDown('slow');
			});

	});
	
	var selectedDate = new Date();
		var cal = Calendar.setup({ 
			inputField: "datebegin2"
			,onSelect: function(cal) { 
						var date = this.selection.get();
						 date = Calendar.intToDate(date);						 
						 selectedDate = date;
						date = Calendar.printDate(date, "%Y-%m-%d");
						jQuery("#datebegin2").val(date);
					cal.hide();
					jQuery('.cal-clone').height( '0px');
			}
			,min:$today
		});
		
		//scal.popup("f_btn2");
		
		//jQuery(document).click(function() {
		//	jQuery('.cal-clone').height( '0px');
		//});
								
		cal.manageFields("f_btn2", "datebegin2", "%Y-%m-%d");
	

	//]]></script>			
EOF;
	return $content ;
};



function page1($charr) {
	global $wpdb;
	$content = "";
	if ($_REQUEST['pager']	== 1) {
		list($year, $month, $day) = sscanf($_REQUEST['datebegin'], "%04s-%02s-%02s"); 
		
		$datebegin=$year."-". $month."-". $day;
		
		$dateend=date("Y-m-d",strtotime($datebegin)+($_REQUEST['kolday']-1)*24*60*60);
		
	} else {
		
		$datebegin=$_REQUEST['datebegin'];
		
		$dateend=$_REQUEST['kolday'];
	}
	if ( isset ($_REQUEST['flagkd']) )	{
	$dateend=date("Y-m-d",strtotime($_REQUEST['dateend'])-1*24*60*60);
	$kolday=$_REQUEST['kolday']=(strtotime($_REQUEST['dateend'])-strtotime($_REQUEST['datebegin']))/(24*60*60);}

	//$content .= "<h2 class='title'>".__("Availability bikes","mb-reservation")."</h2>";

	$qry="SELECT a.resourceid, name ,(if ('$datebegin' >= d.datebegin and '$dateend'<= d.dateend, d.price, a.price)) price, price1, price2,  capacity, calcType  
	";

	$dt=strtotime ($datebegin);
	$bt = '<form name="res_order" id="res_order"  action="" method="post" >
	<table width=100% class="res reservation" > <thead>	<tr >
	<td style=\"width:200px\" > </td>';
	while (strtotime($dateend)>=$dt)  {
		$qdt=date("Y-m-d",$dt);
		$qdtout=date("d/m",$dt);

		$qry.=",    capacity - sum(if ( '$qdt' between c.datebegin and c.dateend, kol, 0)) `$qdt`  ";
		$bt .= "<td  >available on $qdtout</td>";
		$dt=$dt+24*60*60;
	}
	$bt .= '<td>'.__("price","mb-reservation").'</td>'
			.'<td>'.__("price children","mb-reservation").'</td>'
			.'<td>'.__("price students","mb-reservation").'</td><td>'
			.__("Number #bikes","mb-reservation").'</td></tr></thead>';
		
	$qry.=pro_page1_qry($charr); 
	//die();
	/*else {
		$qry.="   from  
		".$wpdb->prefix . "res_orders  c
	INNER JOIN  ".$wpdb->prefix . "res_orders_content   b 
	ON (c.orderid =
		b.orderid)
		RIGHT JOIN  ".$wpdb->prefix . "res_resources   a ON (b.resourceid =
		a.resourceid)
		LEFT JOIN ".$wpdb->prefix . "res_offers d ON (d.resourceid = a.resourceid)
		where capacity>0 
		group by name, a.resourceid, price ";
	
	}
	*/

	//echo( $qry );
	$results=$wpdb->get_results($wpdb->prepare($qry), ARRAY_N );

	$rowflag=1;  $j=1;
	if (count($results)>0) {
		
		$content .= $bt;
		foreach ($results as $fld) {
			$content .= '<tr class="row'.$rowflag.'">';
			$k=1;
			$min=$fld[3];
			foreach ($fld as $fld2) {

				switch ($k) {
				case 1:   $resource=$fld2; break;
				case 2: $content .= "<td >".$fld2."</td>";
					$_SESSION['mb-booking']['name'] = $fld2;
					break;
				case 3: $price = $fld2;  break;
				case 4: $price1 = $fld2;  break;
				case 5: $price2 = $fld2;  break;
				
				case 6: $capacity = $fld2;  break;
				case 7: $calcType = $fld2;  break;

//				case 4: $minorder = $fld2;  break;       
				default:  if ($capacity==1) {if ($fld2==1) $content .= "<td ><img src=\"".RES_PLUGIN_URL."/js/jQTreeTable/images/tick.png\"></td>"; else $content .= "<td ><img src=\"".RES_PLUGIN_URL."/js/jQTreeTable/images/del.png\"></td>";} else {$content .= "<td >$fld2</td>";} if ($fld2<$min) $min=$fld2; break;
				}
				$k++;
			}
			if ($capacity < $_REQUEST['numberbikes'])
				$errmsg = "Not enough bikes available";
			else
				$errmsg = "";

			$content .= '<td>'.$price.'</td><td>'.$price1.'</td><td>'.$price2.'</td>
			<td>
			<input type=hidden name="price['.$resource.']" value="'.$price.'">
			<input type=hidden name="price1['.$resource.']" value="'.$price1.'">
			<input type=hidden name="price2['.$resource.']" value="'.$price2.'">
			<input type=hidden name="calcType['.$resource.']" value="'.$calcType.'">
			<input type=hidden name=resource_name['.$resource.'] value="'.$fld[1].'">';
						
			if ($min==1) {$content .= '<input type="checkbox" name="reskol['.$resource.']" value="1">'; 
			if ($j>1) $javaif .= " && (obj.elements['reskol[$resource]'].checked==false) "  ; else $javaif = " (obj.elements['reskol[$resource]'].checked==false) ";
			}
			else 
			//if ($minorder<=$min ) 
			if  ($min>0)
			{ 
				if (false) {
					$content .= '<select col=30  name=reskol['.$resource.']> <option value="0">'.__("Select").'</option>   ';
					if ($j>1) $javaif .= " && (obj.elements['reskol[$resource]'].value==0) "  ; else $javaif = " (obj.elements['reskol[$resource]'].value==0) ";
					for ($i=1;$i<=$min; $i++) {
						if ($i == $_REQUEST['numberbikes'])
							$sel = "selected";
						else
							$sel = "";
						$content .= '<option '.$sel.' value="'.$i.'">'.$i.'</option>';			
					}
					$content .= '</select>' ; 
				}
				else {
					$content .= '<input class="small" type=text name=reskol['.$resource.'] value='. $_REQUEST['numberbikes'].'>';
				}
			} 
			
			$content .= '</td></tr>';			
			$j++;
			$rowflag = $rowflag*(-1);

		}

		if (!empty($errmsg)) {
			$contButton = '<p class="info">'.__("Sorry there are not enough bikes or places on the tour available on one of these days, please choose another date or tour.","mb-reservation").'</p>';
			
			$autoContinue = "jQuery('table.reservation').hide(); ";
		}
		else {
			$contButton = '<input class="res-next" id="button"  type="submit" value="'.__("Ok","mb-reservation").'"   >';
			$autoContinue = "jQuery('form#res_order').submit();";
		}
		$content .= '</table>
		<div align=left>
		'.$contButton.'		
		<input class="res-back" id="button"  onclick="javascript:history.go(-1);" type="button" value="'.__("Back","mb-reservation").'">
		<input type="hidden" name=pager value="2" >
		<input type="hidden" name="datebegin" value="'.$datebegin.'" ><input type="hidden" name="dateend" value="'.$dateend.'" >
		<input type="hidden" name="kolday" value="'.$_REQUEST['kolday'].'" > 
		</div>
		</form>';			

		$content .= <<<EOF
		<div id="error" title="
EOF
.__("Error","mb-reservation").
<<<EOF
">
</div>
	<script language="javascript">
	jQuery(function() {
		{$autoContinue}
		jQuery( "#error" ).dialog({
			bgiframe: true,
			height: 120,
			modal: true,
			autoOpen: false
		});
	});		
-->
</script> 
EOF;
		
		
	}
	else $content .= "<p>".__("No information about free resources","mb-reservation")."</p>";
	return $content ;
}

function page2a() {
	global $wpdb;
	$content = "";
	
	//print_r($_REQUEST);

	$dateend=date("Y-m-d",strtotime($datebegin)+($_REQUEST['dateend']-1)*24*60*60);
	$datebegin=date("Y-m-d",strtotime($_REQUEST['datebegin']));
	$dateend=date("Y-m-d",strtotime($_REQUEST['dateend']));
		
	//$days = ceil(abs(strtotime($_REQUEST['dateend'])-strtotime($_REQUEST['datebegin']))  / 86400);
	$sess = $_SESSION['mb-booking'];
	
	$content .= '<p>'.__("Please check and confirm your reservation","mb-reservation").'</p>'.
	'<style>'.
	'td.confirm { width: 200px; text-align: left; color: #000;} '.
	'.res th { border-bottom: 1px solid #ababab; } '.
	'</style><table class="res" width="400" >';
/*
	<thead>
	<tr >
	<th width=100>'.__("Tour/Bike Rental","mb-reservation").'</th>
	<th width=100>'.__("Persons","mb-reservation").'</th>
	<th width=100>'.__("Price/day","mb-reservation").'</th>
	</tr>
	</thead>
*/
	//var_dump($sess);
	
	//var_dump($_REQUEST);
	$reskol=$_REQUEST['reskol'];
	$price=$_REQUEST['price'];
	$price1=$_REQUEST['price1'];
	$price2=$_REQUEST['price2'];
	$calcType = $_REQUEST['calcType'];
	$kolday=$_REQUEST['kolday'];
	$resource_name=$_REQUEST['resource_name'];
	$rowflag=1; $sumprice=0;
	$daypricing = false;
	$tour = null;
	// er kan maar voor 1 tour/product worden gekozen
	foreach ($_REQUEST['reskol'] as $key=>$value )
	{
		if ($value>0) {
			if ($calcType[$key] == 0) {
				$sumprice+= ($sess['grownups']*$price[$key] + $sess['children']*$price1[$key] + $sess['students']*$price2[$key] )*$kolday;
				$extraline = "";
				$aReduced = array('children','student');
			}
			else {
				$tour = $key;
				$daypricing = true;
				$extraline = "<tr><th>Pickup Time</th><td class=confirm>".$sess['time']."</td></tr>";
				$aReduced = array('>3d','>week');
			}		
			/*
			$prices = "<tr><th>Price/day</th><td class=confirm>".$price[$key]."</td></tr>".
			str_replace(array('[1]','[2]'), $aReduced,"<tr><th>Price [1] / day</th><td class=confirm>".$price1[$key]."<td></tr><tr><th>Price [2] / day</th><td class=confirm>".$price2[$key]."</td></tr>");
			*/
			$prices = "";
			
			$content .= "<tr class=\"row$rowflag\" ><th>Tour/Bike Rental</th><td class=confirm>".$resource_name[$key]."</td></tr>".
			"<tr><th>Date</th><td class=confirm>".$datebegin." / ".$dateend."</td></tr>".
			$extraline.
			"<tr><th>Number bikes</th><td class=confirm>".$value."</td></tr>".
			$prices;
			
			$rowflag = $rowflag*(-1); 
			//$sumprice+=$reskol[$key]*$price[$key]*$kolday;
		}
	}
	if ($daypricing) {
		if ($kolday < 3) {
			$sumprice = $kolday * $price[$tour];
		}
		elseif ($kolday >= 3 && $kolday < 7) {
			$sumprice = $kolday * $price1[$tour];
		}
		elseif ($kolday >= 7) {
			$sumprice = $kolday * $price2[$tour];
		}
		$sumprice *= $sess['grownups'] + $sess['children'] + $sess['students'];
	}
	$_SESSION['mb-booking']['sumprice'] = $sumprice;
	$content .= "<tr class=\"row$rowflag\" ><th>".__("Total","mb-reservation")." </th><td class=confirm>&euro; $sumprice</td></tr>";
	$content .= "</table>";
	
	$hidden="";		
	$hiddenget="?nvar=0";		
	$sumprice=0;
	foreach ($_REQUEST['reskol'] as $key=>$value )
	{
		if ($value>0) {
			$sumprice+=$reskol[$key]*$price[$key]*$kolday;
			$hidden .= "<input type=\"hidden\" name=\"reskol[$key]\" value=\"$value\" >";
			$hidden .= "<input type=\"hidden\" name=\"price[$key]\" value=\"".$price[$key]."\" >";
			$hidden .= "<input type=\"hidden\" name=\"resource_name[$key]\" value=\"".$resource_name[$key]."\" >";
			
			$hiddenget .= "&reskol[$key]=".urlencode($value);
			$hiddenget .= "&price[$key]=".urlencode($price[$key]);
			$hiddenget .= "&resource_name[$key]=".urlencode($resource_name[$key]);
		}
		
	}
	
	$hidden .= "<input type=\"hidden\" name=pager value=\"2b\" >";
	$hidden .= "<input type=\"hidden\" name=\"datebegin\" value=\"".$_REQUEST['datebegin']."\" >";
	$hidden .= "<input type=\"hidden\" name=\"dateend\" value=\"".$_REQUEST['dateend']."\" >";
	$hidden .= "<input type=\"hidden\" name=\"kolday\" value=\"".$_REQUEST['kolday']."\" >";
	$hiddenget .= "&datebegin=".urlencode($_REQUEST['datebegin']);
	$hiddenget .= "&dateend=".urlencode($_REQUEST['dateend']);
	$hiddenget .= "&kolday=".urlencode($_REQUEST['kolday']);
	$hiddenget .= "&pager=3";

	$content .= "
	<p>
	    <a href='".get_option("res_pay_paypal_return")."'><input class='res-back' id='button'  type='button' value='".__("Back","mb-reservation")."'></a>	
		<a href='". $_SERVER['REQUEST_URI'].($hiddenget)."'><input type=button class=res-next style='color:#fff!important; float: left;' name='OK' value='OK' ></a>			
	</p>";	
	return $content;
}


function page2b() {
	global $wpdb;
	$content = "";

	$hidden="";		
	$hiddenget="?nvar=0";		
	$reskol=$_REQUEST['reskol'];
	$price=$_REQUEST['price'];
	$resource_name=$_REQUEST['resource_name'];
	
	foreach ($_REQUEST['reskol'] as $key=>$value )
	{
		
		$sumprice+=$reskol[$key]*$price[$key]*$kolday;
		$hidden .= "<input type=\"hidden\" name=\"reskol[$key]\" value=\"$value\" >";
		$hidden .= "<input type=\"hidden\" name=\"price[$key]\" value=\"".$price[$key]."\" >";
		$hidden .= "<input type=\"hidden\" name=\"resource_name[$key]\" value=\"".$resource_name[$key]."\" >";
		
		$hiddenget .= "&reskol[$key]=".urlencode($value);
		$hiddenget .= "&price[$key]=".urlencode($price[$key]);
		$hiddenget .= "&resource_name[$key]=".urlencode($resource_name[$key]);
		
		
	}
	$hidden .= "<input type=\"hidden\" name=pager value=\"3\" >";
	$hidden .= "<input type=\"hidden\" name=\"datebegin\" value=\"".$_REQUEST['datebegin']."\" >";
	$hidden .= "<input type=\"hidden\" name=\"dateend\" value=\"".$_REQUEST['dateend']."\" >";
	$hidden .= "<input type=\"hidden\" name=\"kolday\" value=\"".$_REQUEST['kolday']."\" >";
	$hiddenget .= "&datebegin=".urlencode($_REQUEST['datebegin']);
	$hiddenget .= "&dateend=".urlencode($_REQUEST['dateend']);
	$hiddenget .= "&kolday=".urlencode($_REQUEST['kolday']);
	$hiddenget .= "&pager=3";
	$hiddenget .= "&alreadyreg=1";

	
	
	$dateend=date("Y-m-d",strtotime($datebegin)+($_REQUEST['dateend']-1)*24*60*60);
	$datebegin=date("d.m.Y",strtotime($_REQUEST['datebegin']));
	$dateend=date("d.m.Y",strtotime($_REQUEST['dateend']));

	$content .= '<br>'.sprintf(__("Your order from %s to %s ","mb-reservation"),$datebegin,$dateend).'	
	<table class="res" width="400" >
	<thead>
	<tr >
	<th width=100>'.__("resource","mb-reservation").'</th>
	<th width=100>'.__("quantity","mb-reservation").'</th>
	<th width=100>'.__("price","mb-reservation").'</th>
	</tr>
	</thead>
';



	$rowflag=1; $sumprice=0;
	$kolday=$_REQUEST['kolday'];
	
	foreach ($_REQUEST['reskol'] as $key=>$value )
	{
		$content .= "<tr class=\"row$rowflag\" ><td>".$resource_name[$key]."</td><td>".$value."</td><td>".$price[$key]."</td></tr>";
		$rowflag = $rowflag*(-1); $sumprice+=$reskol[$key]*$price[$key]*$kolday;
	}
	$content .= "<tr class=\"row$rowflag\" ><td colspan=2 >".__("Total","mb-reservation")."</td><td>$sumprice</td></tr>";
	$content .= "</table>";
	
	
	require_once ( ABSPATH . WPINC . '/registration.php' );	
	
	$user_info = email_exists($_REQUEST['log']);
	//print_r($user_info->user_login);
	if ($user_info)
	
	{
		$user_info=get_userdata($user_info);
		
		$hiddenget .= "&alreadyreg=1";
		$content .= sprintf(__('<p>Mail us <b>% s </b> is already registered in the system, enter your password and click "Login"</p>',"mb-reservation"),$_REQUEST['log']); 
		$content .= '<form action="'. get_option("home").'/wp-login.php" method="post">

	<table width=400  >

	<tr><td><label for="pwd">'.__("Password","mb-reservation").'</label></td><td><input type="hidden" name="log" id="log" value="'. wp_specialchars(stripslashes($user_info->user_login), 1) .'" size="20" /><input type="password" name="pwd" id="pwd" size="20" /></td></tr>

	<tr><td></td><td><input type="submit" id="button" name="submit" value="'.__("Login","mb-reservation").'" class="res-next" /></td></tr></table>

	<p>
	<input name="rememberme" id="rememberme" type="hidden"  value="forever" /> 
	
	<input type="hidden" name="redirect_to" value="'. $_SERVER['REQUEST_URI'] .''.($hiddenget).'" />
	</p> 
'.$hidden.'	 
</form>
<a href="'. get_option('home'). '/wp-login.php?action=lostpassword">'.__("Lost Password","mb-reservation").'</a>';


		
	}	
	else 
	{
		
if ($GLOBALS["version"]	== "pro") $check_fields_js=check_fields_js();

		$content .= <<<EOF
		<div id="error" title="
EOF
.__("Error","mb-reservation").
<<<EOF
">
</div>
<div id="term" title="
EOF
.__("Terms of booking","mb-reservation").
<<<EOF
">
EOF
.get_option("res_terms").
<<<EOF
</div>
		<script language="javascript">
jQuery(function() {
		jQuery( "#error" ).dialog({
						bgiframe: true,
			height: 220,
			modal: true,
			autoOpen: false

		});
		jQuery( "#term" ).dialog({
						bgiframe: true,
			height: 320,
			width: 450,
			modal: true,
			autoOpen: false

		});
        jQuery( "#aterm" ).click(function() {
		jQuery( "#term" ).dialog( "open" );})
		
		jQuery( "#button" ).click(function() {
	
			var err = "";
			var obj = document.forms['res_order'];
			if (obj.last_name.value=="") {
				err += "
EOF
.__(" - Not specified (Name)","mb-reservation").
<<<EOF
			<br>";
			}
EOF
.
$check_fields_js
.			
<<<EOF
			
			if (obj.rules.checked==false) {
				err += "
EOF
.__(" - You must agree to the terms","mb-reservation").
<<<EOF
		<br>	";
			}

			if (err!="") {
				err = "" + err;
			document.getElementById("error").innerHTML=err;
			jQuery( "#error" ).dialog( "open" );
			return false; } 
		
		
			
		});
	});

		</script> 
EOF;

		$content .= '<form name="res_order" id="res_order" action="" method="post"> ';
		$content .= $hidden." <br><p>".__("Enter your personal data","mb-reservation")."</p>";
		if ($GLOBALS["version"]	== "pro") $phone_row=phone_row();	
		$content .= "<table class=restabname width=400  >
<tr ><td  width=200>".__("First name","mb-reservation")."</td><td><input type=text name=first_name value=\"".$_REQUEST['first_name']."\"></td></tr>
<tr ><td>".__("Last name","mb-reservation")."</td><td><input type=text name=last_name value=\"".$_REQUEST['last_name']."\"></td></tr>
<tr ><td>".__("Middle name","mb-reservation")."</td><td><input type=text name=middle_name value=\"".$_REQUEST['middle_name']."\"></td></tr>"
.$phone_row."
<tr ><td>".__("Comments","mb-reservation")."</td><td><textarea  name=comments rows=5 cols=10 >".$_REQUEST['comments']."</textarea></td></tr>
<tr ><td><a id=\"aterm\">".__("I agree with the terms of booking","mb-reservation")."</a></td><td><input type=checkbox name=rules></td></tr>
<tr ><td> </td><td><input type=\"submit\" class=\"res-next\" id=\"button\" value=\"".__("next","mb-reservation")."\"  ></td></tr>
</table>
<input type=hidden name=email value=\"".$_REQUEST['log']."\">
</form>

";
	}




	
	return $content;
}


function page3() {
	global $wpdb, $current_user;
	
	//print_r($_SESSION);
	//print_r($_REQUEST);
	$content = "";
	$content .= "<strong>".__("Order created","mb-reservation")."</strong>";
		
	//wp_set_current_user( $userid );
	$userid = $_SESSION['userid'];
	
	$reskol=$_REQUEST['reskol'];
	$price=$_REQUEST['price'];
	$kolday=$_REQUEST['kolday'];
	$resource_name=$_REQUEST['resource_name'];
	
	$order_details = '<table  width="400" >
	<thead>
	<tr >
	<th width=100 align="left">'.__("resource","mb-reservation").'</th>
	<th width=100 align="left">'.__("quantity","mb-reservation").'</th>
	<th width=100 align="left">'.__("price","mb-reservation").'</th>
	</tr>
	</thead>
';		
	$rowflag=1; 
	foreach ($_REQUEST['reskol'] as $key=>$value )
	{
		//$sumprice+=$reskol[$key]*$price[$key]*$kolday;
		if ($value>0) {
			//$order_details .= "<tr  ><td>".$resource_name[$key]."</td><td>".$value."</td><td>".$price[$key]."</td></tr>";
			$order_details .= "<tr  ><td>".$resource_name[$key]."</td><td>".$value."</td><td>".$_SESSION['mb-booking']['sumprice']."</td></tr>";
			
			//$rowflag = $rowflag*(-1); 
			//$sumprice+=$reskol[$key]*$price[$key]*$kolday;
		}
	}
	$sumprice = $_SESSION['mb-booking']['sumprice'];
	$order_details .= "</table>";		
	$comments= 	'tour: '. $_SESSION['mb-booking']['name'].
				' @'. $_SESSION['mb-booking']['time'].
				'\nadults: '. $_SESSION['mb-booking']['grownups'].
				'\nstudents: '. $_SESSION['mb-booking']['students'].
				'\nchildren: '. $_SESSION['mb-booking']['children'];
    if (true) {	
		$qry ="insert into ".$wpdb->prefix . "res_orders"." (userid, price, datebegin, dateend, comments) values ('".$userid."','$sumprice','".$_REQUEST['datebegin']."','".$_REQUEST['dateend']."','".$comments."')";
		
		//echo $qry;
		$wpdb->query($wpdb->prepare($qry) );	
		$orderid= mysql_insert_id();

		foreach ($_REQUEST['reskol'] as $key=>$value)
		{
			if ($reskol[$key]>0 ) {
				$qry ="insert into ".$wpdb->prefix . "res_orders_content"." (orderid, resourceid, kol, price) values ('$orderid','$key','$reskol[$key]','$price[$key]')";
				
				//echo ($qry);
				$wpdb->query($wpdb->prepare($qry) );	
				
			}
		}
		
    }
		
	
	$_REQUEST['orderid']=$orderid;
	$_REQUEST['summa']=$sumprice;
	if (isset($orderid)) $_REQUEST['orderid']=$orderid;
	if (isset($summa)) $_REQUEST['summa']= $sumprice. " euro";

	//$content.="<p>".__('You have to pay now for your order',"mb-reservation")."</p>";
	$content.= payment($_REQUEST['orderid'],$_REQUEST['summa']);

	if (true) {
				add_filter('wp_mail_content_type',create_function('', 'return "text/html";'));
				
				$adm_subject = get_option("res_mail_admin_subject");
				$adm_message = get_option("res_mail_admin_message");
				$client_subject = get_option("res_mail_client_subject");
				$client_message = get_option("res_mail_client_message");
				
				$adm_subject=str_replace('[num]',$orderid,$adm_subject);
				$adm_message=str_replace('[num]',$orderid,$adm_message);
				$client_subject=str_replace('[num]',$orderid,$client_subject);
				$client_message=str_replace('[num]',$orderid,$client_message);
				
				$adm_subject=str_replace('[sum]',$sumprice,$adm_subject);
				$adm_message=str_replace('[sum]',$sumprice,$adm_message);
				$client_subject=str_replace('[sum]',$sumprice,$client_subject);
				$client_message=str_replace('[sum]',$sumprice,$client_message);
				
				$adm_subject=str_replace('[datebegin]',$_REQUEST['datebegin'],$adm_subject);
				$adm_message=str_replace('[datebegin]',$_REQUEST['datebegin'],$adm_message);
				$client_subject=str_replace('[datebegin]',$_REQUEST['datebegin'],$client_subject);
				$client_message=str_replace('[datebegin]',$_REQUEST['datebegin'],$client_message);

				$client_subject=str_replace('[days]',$_REQUEST['kolday'],$client_subject);
				$client_message=str_replace('[days]',$_REQUEST['kolday'],$client_message);
				
				if ($_REQUEST['dateend'] == $_REQUEST['datebegin'] ) {
					$adm_subject=str_replace('[dateend]','',$adm_subject);
					$adm_message=str_replace('[dateend]','',$adm_message);
					$client_subject=str_replace('[dateend]','',$client_subject);
					$client_message=str_replace('[dateend]','',$client_message);				
				}
				else {
					$adm_subject=str_replace('[dateend]',$_REQUEST['dateend'],$adm_subject);
					$adm_message=str_replace('[dateend]',$_REQUEST['dateend'],$adm_message);
					$client_subject=str_replace('[dateend]',$_REQUEST['dateend'],$client_subject);
					$client_message=str_replace('[dateend]',$_REQUEST['dateend'],$client_message);
				}
				
				$adm_subject=str_replace('[login]',$_REQUEST['email'],$adm_subject);
				$adm_message=str_replace('[login]',$_REQUEST['email'],$adm_message);
				$client_subject=str_replace('[login]',$_REQUEST['email'],$client_subject);
				$client_message=str_replace('[login]',$_REQUEST['email'],$client_message);
				
				$adm_subject=str_replace('[password]',$passgen,$adm_subject);
				$adm_message=str_replace('[password]',$passgen,$adm_message);
				$client_subject=str_replace('[password]',$passgen,$client_subject);
				$client_message=str_replace('[password]',$passgen,$client_message);
				
				$adm_subject=str_replace('[first_name]',$_REQUEST['first_name'],$adm_subject);
				$adm_message=str_replace('[first_name]',$_REQUEST['first_name'],$adm_message);
				$client_subject=str_replace('[first_name]',$_REQUEST['first_name'],$client_subject);
				$client_message=str_replace('[first_name]',$_REQUEST['first_name'],$client_message);
				
				$adm_subject=str_replace('[last_name]',$_REQUEST['last_name'],$adm_subject);
				$adm_message=str_replace('[last_name]',$_REQUEST['last_name'],$adm_message);
				$client_subject=str_replace('[last_name]',$_REQUEST['last_name'],$client_subject);
				$client_message=str_replace('[last_name]',$_REQUEST['last_name'],$client_message);
				
				$advance=$sumprice/100*get_option("res_advance_payment_in_percents");
				$adm_subject=str_replace('[advance]',$advance,$adm_subject);
				$adm_message=str_replace('[advance]',$advance,$adm_message);
				$client_subject=str_replace('[advance]',$advance,$client_subject);
				$client_message=str_replace('[advance]',$advance,$client_message);
				
				$adm_subject=str_replace('[order_details]',$order_details,$adm_subject);
				$adm_message=str_replace('[order_details]',$order_details,$adm_message);
				$client_subject=str_replace('[order_details]',$order_details,$client_subject);
				$client_message=str_replace('[order_details]',$order_details,$client_message);		

				$adm_subject=str_replace('[sum]',$sumprice,$adm_subject);
				$adm_message=str_replace('[sum]',$sumprice,$adm_message);
				$client_subject=str_replace('[sum]',$sumprice,$client_subject);
				$client_message=str_replace('[sum]',$sumprice,$client_message);		
				
				$s = $_SESSION['mb-booking'];
				$number = (intval($s['grownups']) + intval($s['children']) + intval($s['students'])). '';
				$adm_subject=str_replace('[number]',$number,$adm_subject);
				$adm_message=str_replace('[number]',$number,$adm_message);
				$client_subject=str_replace('[number]',$number,$client_subject);
				$client_message=str_replace('[number]',$number,$client_message);		

				$adm_subject=str_replace('[name]',$s['name'],$adm_subject);
				$adm_message=str_replace('[name]',$s['name'],$adm_message);
				$client_subject=str_replace('[name]',$s['name'],$client_subject);
				$client_message=str_replace('[name]',$s['name'],$client_message);		

				$em = $_SESSION['email'];
				$adm_subject=str_replace('[email]',$em,$adm_subject);
				$adm_message=str_replace('[email]',$em,$adm_message);
				$client_subject=str_replace('[email]',$em,$client_subject);
				$client_message=str_replace('[email]',$em,$client_message);		
												
				
				pro_confirm_mail($userid, $adm_subject , $adm_message,  $client_subject , $client_message );
				
	}	
	else 
	{
		if (!$refflag) 
		{
			wp_mail(  get_userdata( $userid )->user_email , __("Registration on ","mb-reservation").  get_bloginfo('name') , sprintf(__("Thank you for your order! For your order to create an account. Your Login %s Your password %s","mb-reservation"),$_REQUEST['email'],$passgen));
		}
	}

	return $content;	
}

function payment($orderid,$sumprice)
{
//$total_amount=$sumprice;
//$sumprice=$sumprice/100*get_option("res_advance_payment_in_percents");
	
//if (get_option("res_advance_payment_in_percents")!="100") {
if (true) {
	$content .= "<p>".
	sprintf(__("Thanks for your reservation (order number is %s). After payment of &euro; %s you will receive an email with your reservation details. If you would like to change or cancel please call or send us an email at:","mb-reservation" ),$orderid, $sumprice);	
	
	$content .= "<br>+31(0)20 622 7970 or mikesbiketoursamsterdam@yahoo.com	
	</p>";
}
	/*
	if ( get_option("res_pay_paypal_enabled") && !get_option("res_pay_pos_enabled") && !get_option("res_pay_robokassa_enabled") && !get_option("res_pay_sber_enabled") && !get_option("res_pay_webmoney_enabled") && !get_option("res_pay_mobcash_enabled") ) 
	*/
	$content .= paypal(); 
	/*
	else
	if ( !get_option("res_pay_paypal_enabled") && get_option("res_pay_pos_enabled") && !get_option("res_pay_robokassa_enabled") && !get_option("res_pay_sber_enabled") && !get_option("res_pay_webmoney_enabled") && !get_option("res_pay_mobcash_enabled") ) $content .= wp_redirect(payonlinesystem()); 
	else
	if ( !get_option("res_pay_paypal_enabled") && !get_option("res_pay_pos_enabled") && get_option("res_pay_robokassa_enabled") && !get_option("res_pay_sber_enabled") && !get_option("res_pay_webmoney_enabled") && !get_option("res_pay_mobcash_enabled") ) $content .= robokassa (); 
	else
	if ( !get_option("res_pay_paypal_enabled") && !get_option("res_pay_pos_enabled") && !get_option("res_pay_robokassa_enabled") && get_option("res_pay_sber_enabled") && !get_option("res_pay_webmoney_enabled") && !get_option("res_pay_mobcash_enabled") ) $content .= sber (); 
	else
	if ( !get_option("res_pay_paypal_enabled") && !get_option("res_pay_pos_enabled") && !get_option("res_pay_robokassa_enabled") && !get_option("res_pay_sber_enabled") && get_option("res_pay_webmoney_enabled") && !get_option("res_pay_mobcash_enabled") ) $content .= webmoney (); 
	else
	if ( !get_option("res_pay_paypal_enabled") && !get_option("res_pay_pos_enabled") && !get_option("res_pay_robokassa_enabled") && !get_option("res_pay_sber_enabled") && !get_option("res_pay_webmoney_enabled") && get_option("res_pay_mobcash_enabled") ) $content .= mobcash (); 
	
	else 

	{
	$content .= <<<EOF
			
	<form name="res_order" id="res_order" method="post" action="">
	<input type="hidden" name=summa value="$sumprice" >
	<input type="hidden" name=orderid value="$orderid" >
	<input type="hidden" name="pager" value="nopay" />
EOF;

	
	pro_paysys_list();
	
	$content .= '</form>
	
	';
		$content .= <<<EOF
<div id="error" title="
EOF
.__("Error","mb-reservation").
<<<EOF
">
</div>
	<script language="javascript">
// increase the default animation speed to exaggerate the effect
	
	jQuery(function() {
		jQuery( "#error" ).dialog({
						bgiframe: true,
			height: 120,
			modal: true,
			autoOpen: false

		});

		jQuery( "#button" ).click(function() {
		
		var err = "";
		var obj = document.forms['res_order'];

		
		//alert(obj.pager.length);
		for (i=0;i<obj.pager.length;i++){
			//alert(obj.pager[i].value);
			if (obj.pager[i].checked==true) {
				err += "1";
			}
		}

		if (err!="") {
			return true;
		} else {
			err = "
EOF
.__("You have not chosen a payment system","mb-reservation").
<<<EOF
			" ;
			document.getElementById("error").innerHTML=err;
			jQuery( "#error" ).dialog( "open" );
			return false; } 
		
		
			
		});
	});
	
	
	</script> 
EOF;
	
	}
	*/
	return $content;
}

function pageI23($interf) {

	$today = date("Ymd");  $today2 = date("Y-m-d"); 
	list($results,$map,$treet,$level)=treesort();
	$content = "";
	$treeselect=treeselect($results,$level,$interf);
	$content .= <<<EOF
	<p>
EOF
	.__('To see prices and availability, enter your arrival date and how many days you will use our resources.', 'mb-reservation').
	<<<EOF
	</p>
<form name="res_order"  action="" method=post ><input type="hidden" name="pager" value="1" >
<input type="hidden" id="datebegin2" name="datebegin" value="" /> 
<input type="hidden" id="dateend" name="dateend" class="koldays" >	
<input type="hidden" id="flagkd" name="flagkd"   value="1" >	
<table>
	<tr>
		<td class="res-I2-td1">
			<div id="cont1"></div>
			<div id="info" style="text-align: center; font-size: 11px">
EOF
.__("Click to select arrival date","mb-reservation").
<<<EOF
</div>
		</td>
		<td class="res-I2-td2"><b>	
EOF
	.__("Location","mb-reservation"). 
<<<EOF
</b>
<br>
EOF
.
$treeselect
.
<<<EOF
	<br><br><input class="res-next" id="button" type="submit" value="
EOF
.__("next","mb-reservation").
<<<EOF
">
		</td>
	</tr>
</table>
<div id="error" title="
EOF
.__("Error","mb-reservation").
<<<EOF
">
</div>

	<script type="text/javascript">//<![CDATA[

	var SELECTED_RANGE = null;
	function getSelectionHandler() {
		var startDate = null;
		var ignoreEvent = false;
		return function(cal) {
			var selectionObject = cal.selection;

			// avoid recursion, since selectRange triggers onSelect
			if (ignoreEvent)
			return;

			var selectedDate = selectionObject.get();
			if (startDate == null) {
				startDate = selectedDate;
				SELECTED_RANGE = null;
				document.getElementById("info").innerHTML = "
EOF
.__("Click to select departure date","mb-reservation").
<<<EOF
				";
				date1 = Calendar.intToDate(startDate);
				document.getElementById("datebegin2").value = Calendar.printDate(date1,"%Y-%m-%d");
				document.getElementById("dateend").value = "";
				// comment out the following two lines and the ones marked (*) in the else branch
				// if you wish to allow selection of an older date (will still select range)
				cal.args.min = Calendar.intToDate(selectedDate);
				cal.refresh();
			} else {
				ignoreEvent = true;
				selectionObject.selectRange(startDate, selectedDate);
				ignoreEvent = false;
				SELECTED_RANGE = selectionObject.sel[0];
				date2 = Calendar.intToDate(selectedDate);
				document.getElementById("info").innerHTML = "
EOF
.__("Click again to select new arrival date","mb-reservation").
<<<EOF
";
				document.getElementById("dateend").value = Calendar.printDate(date2,"%Y-%m-%d");
				startDate = null;

				// (*)
				cal.args.min = null;
				cal.refresh();
				
			}
		};
	};

	Calendar.setup({
		cont          : "cont1",
		fdow          : 1,
		selectionType : Calendar.SEL_SINGLE,
		onSelect      : getSelectionHandler(),
		min			: $today
	});

	
	jQuery(function() {
		jQuery( "#error" ).dialog({
bgiframe: true,
height: 150,
modal: true,
autoOpen: false

		});

		jQuery( "#button" ).click(function() {	  
			var err = "";
			var obj = document.forms['res_order'];
			if (obj.datebegin2.value=="") {
				err += "
EOF
.__(" - Not specified Arrival date","mb-reservation").
<<<EOF
		<br>";
			}

if (obj.dateend.value=="") {
				err += "
EOF
.__(" - Not specified Departure date","mb-reservation").
<<<EOF
			";
			}			
			
			if (err!="") {
			document.getElementById("error").innerHTML=err;
			jQuery( "#error" ).dialog( "open" );
				return false;} 
			
		});
	});
	//]]></script>	
	
	
</form>	
	
	
EOF;
	

	return $content ;
};	
function pageI4($interf) {

	$today = date("Ymd");  $today2 = date("Y-m-d"); 
	list($results,$map,$treet,$level)=treesort();
	$content = "";
	//print_r($level);
	$treeselect=treeselect($results,$level,$interf);
	
	$content .= <<<EOF
	<p>
EOF
	.__('To see prices and availability, enter your arrival date, departure date and select resort and room.', 'mb-reservation').
	<<<EOF
	</p>
<form name="res_order"  action="" method=post ><input type="hidden" name="pager" value="1" >
<input type="hidden" id="datebegin2" name="datebegin" value="$today2" /> 
<input type="hidden" id="dateend" name="dateend"   value="$today2" >	
<input type="hidden" id="flagkd" name="flagkd"   value="1" >	

<table>
	<tr>
		<td class="res-I4-td1">
			<div id="cont1"></div>
			<div id="info" style="text-align: center; font-size: 11px">
EOF
	.__('Click to select arrival date', 'mb-reservation').
	<<<EOF
	</div>
			<td "res-I4-td2">
			<div id="cont2"></div>
			<div id="info" style="text-align: center; font-size: 11px">
EOF
	.__('Click to select departure date', 'mb-reservation').
	<<<EOF
	</div>
			</td>
			
		</td>
		<td class="res-I4-td3"><b>	
EOF
	.__("Resort","mb-reservation"). 
<<<EOF
</b>
<br>
EOF
.
$treeselect
.
<<<EOF
	<div id="select2"></div>
	<br><br><input class="res-next" id="button" type="submit" value="
EOF
.__("next","mb-reservation").
<<<EOF
">
		</td>
	</tr>
</table>
<div id="error" title="
EOF
.__("Error","mb-reservation").
<<<EOF
">
</div>

<script type="text/javascript">
jQuery('#sel1').change(function() {
jQuery("#select2").load("
EOF
.RES_CUR_PAGE."/?pager=selectajax".
<<<EOF
");

});

</script>

	<script type="text/javascript">//<![CDATA[

	var SELECTED_RANGE = null;
	function getSelectionHandler( cont) {
		var startDate = null;
		var ignoreEvent = false;
		return function(cal) {
			var selectionObject = cal.selection;

			// avoid recursion, since selectRange triggers onSelect
			if (ignoreEvent)
			return;
			
			var selectedDate = selectionObject.get();
			startDate = selectedDate;
			date1 = Calendar.intToDate(startDate);
			
			if (cont==1) {
			
			
			document.getElementById("datebegin2").value = Calendar.printDate(date1,"%Y-%m-%d");
			//document.getElementById("datebegin3").value = Calendar.printDate(date1,"%Y%m%d");
			date2 = Calendar.dateToInt(document.getElementById("datebegin4").value);
			//if (document.getElementById("datebegin4").value=="") document.getElementById("kolday").value = 1; else //document.getElementById("kolday").value = date2-startDate+1;
			
			} else
			{
			
			date2 = Calendar.dateToInt(document.getElementById("dateend").value);
			//document.getElementById("kolday").value = startDate-date2+1;
			document.getElementById("dateend").value = Calendar.printDate(date1,"%Y-%m-%d");
			//document.getElementById("kolday").value = date2;
			
			}
			

		};
	};

	Calendar.setup({
		cont          : "cont1",
		fdow          : 1,
		selectionType : Calendar.SEL_SINGLE,
		onSelect      : getSelectionHandler(1),
		min			: $today
	});
	Calendar.setup({
		cont          : "cont2",
		fdow          : 1,
		selectionType : Calendar.SEL_SINGLE,
		onSelect      : getSelectionHandler(2),
		min			: $today
	});

	
	jQuery(function() {
		jQuery( "#error" ).dialog({
bgiframe: true,
height: 150,
modal: true,
autoOpen: false

		});

		jQuery( "#button" ).click(function() {	  
		
			var err = "";
			var obj = document.forms['res_order'];
			//alert("111"+jQuery( "#datebegin2" ).val()+"111");
			if (jQuery( "#datebegin2" ).val()=="") {
				err += "
EOF
.__(" - Not specified Arrival date","mb-reservation").
<<<EOF
		<br>";
		
			}

if (obj.dateend.value=="") {
				err += "
EOF
.__(" - Not specified Departure date","mb-reservation").
<<<EOF
			";
			}			
			
			if (err!="") {
			jQuery("#error").html(err);
			jQuery( "#error" ).dialog( "open" );
				return false;} 
			
		});
	});
	//]]></script>	
	
	
</form>	
	
	
EOF;
	

	return $content ;
};	
	
  /////////////////////////////////////////////////////////////////////////////
 //					Admin page section										//
/////////////////////////////////////////////////////////////////////////////


function menu_admin () {
	global $wpdb;
	$qry="select count(resourceid)   from  ".$wpdb->prefix . "res_resources";
	$resources=$wpdb->get_var($wpdb->prepare($qry) );
	
	$qry="select count(orderid)   from  ".$wpdb->prefix . "res_orders";
	$ordersall=$wpdb->get_var($wpdb->prepare($qry) );

	$qry="select count(orderid)   from  ".$wpdb->prefix . "res_orders where payed>0";
	$orderspayed=$wpdb->get_var($wpdb->prepare($qry) );
	
	$qry="select count(id)   from  ".$wpdb->prefix . "res_offers";
	$sp_offers=$wpdb->get_var($wpdb->prepare($qry) );


	$pageadr=$_REQUEST['page'];
	if ($_REQUEST["page"]) $divid=$_REQUEST["page"]; else $divid="orders";
	if ($_REQUEST["page"]=="suborder" || $_REQUEST["page"]=="mail") $divid="orders";
	if ($_REQUEST["page"]=="makeorder1" || $_REQUEST["page"]=="makeorder2")  $divid="makeorder";
	$content = '
	<div id="'.$divid.'">
	<br>
	<ul id="tabnav">
	<li class="tab1"><a href="?page=ordertable">'.__("Orders table","mb-reservation").'</a></li>
	<li class="tab2"><a href="?page=orders">'.sprintf(__("Orders (total: %s | paid: %s)","mb-reservation"),$ordersall,$orderspayed).'</a></li>
	<li class="tab3"><a href="?page=makeorder">'.__("Reserve","mb-reservation").'</a></li>
	<li class="tab4"><a href="?page=resources">'.__("Resources","mb-reservation").' ('.$resources.')</a></li>

	<li class="tab6"><a href="?page=settings">'.__("Settings","mb-reservation").'</a></li> ';
	if ($GLOBALS["version"]	!= "pro") $content .='<li class="tab6"><a href="?page=pro"><img src="'.RES_PLUGIN_URL.'/img/pro.gif"> '.__("version","mb-reservation").'</a></li>';
	$content .='</ul>
	</div>
';
/*
 <li class="tab5"><a href="?page=offers">'.__("Special offers","mb-reservation").' ('.$sp_offers.')</a></li>
*/	
	return 	$content;
}



function adm_settings()
{
	global $wpdb;

	$content="";

	
	if ($_REQUEST['change_settings']) {

		update_option("res_days",$_REQUEST["res_days"]) ;
		update_option("res_color",$_REQUEST["res_color"]) ;
		update_option("res_calendar_color",$_REQUEST["res_calendar_color"]) ;
		update_option("res_security_plugin",$_REQUEST["res_security_plugin"]) ;
		update_option("res_security_settings",$_REQUEST["res_security_settings"]) ;
		update_option("res_full_uninstall",$_REQUEST["res_full_uninstall"]) ;
		update_option("res_terms",$_REQUEST["res_terms"]) ;
		update_option("res_calc",$_REQUEST["res_calc"]) ;
		
		if ($GLOBALS["version"] == "pro")	{ pro_update_settings(); }
		
		echo "<div class=alert>".__("Settings saved","mb-reservation")."</div>"		 ;
		
	}

	$role_list= array ("Administrator"=>10,"Editor"=>7,"Author"=>2,"Contributor"=>1,"Subscriber"=>0);

	$content .= '
	<h2>'.__("Settings","mb-reservation").'</h2>
	<form id="form1" name="form1" method="post" action="">	';
	
	$content .= '
	<br>
	<h3>'.__("Design settings","mb-reservation").'</h3>
	<form id="form1" name="form1" method="post" action="">
	<table class="widefat page fixed" style="width:650px">
	<thead>
	<tr >
	<th >'.__("Parameter","mb-reservation").'</th>
	<th>'.__("Value","mb-reservation").'</th>
	</tr>
	</thead>
	<tr>
	<td>'.__("How many days to show for choosing","mb-reservation").'</td>
	<td><select name="res_days" class="koldays" id="settings">';

	$maxdays=31;
	for($i=1;$i<=$maxdays;$i++)
	{
	if ($i==get_option("res_days")) $adder="selected" ; else $adder="";
	$content .= "<option $adder value=\"$i\" >$i</option>";}	
	$content .= '
	</select></td>	
	</tr>
	<tr>
	<td>'.__("Plugin colors (CSS)","mb-reservation").'</td>
	<td><select name="res_color" id="settings">
	
	';
	$adder="";
	$pathcss=RES_PLUGIN_PATH.'/css/res/';
	if ($handle = opendir($pathcss)) {
		while (false !== ($file = readdir($handle))) { 
			if (is_dir($pathcss.$file) && $file !== '.' && $file !== '..') {  
			if ($file==get_option("res_color")) $adder="selected" ; else $adder="";
	$content .= '<option '.$adder.'>'.$file;
			}
		}
	closedir($handle);
	}

	//<td><input type="text" name="res_color"  size=49  value="'.get_option("res_color") .'" /></td>
	$content .= '</select></td>	
	</tr>
	<tr>
	<td>'.__("Calendar colors (CSS)","mb-reservation").'</td>
	<td><select name="res_calendar_color" id="settings">
	';
	$pathcss=RES_PLUGIN_PATH.'/css/calendar/';
	if ($handle = opendir($pathcss)) {
		while (false !== ($file = readdir($handle))) { 
			if (is_dir($pathcss.$file) && $file !== '.' && $file !== '..' && $file !== 'img') {  
			if ($file==get_option("res_calendar_color")) $adder="selected" ; else $adder="";
	$content .= '<option '.$adder.'>'.$file;
			}
		}
	closedir($handle);
	}
	//<td><input type="text" name="res_calendar_color"  size=49  value="'.get_option("res_calendar_color") .'" /></td>
$content .= '	
	</select></td>	
	</tr></table>
	<br>
	<h3>'.__("Security settings","mb-reservation").'</h3>
	<table class="widefat page fixed" style="width:650px">
	<thead>
	<tr >
	<th >'.__("Parameter","mb-reservation").'</th>
	<th>'.__("Value","mb-reservation").'</th>
	</tr>
	</thead>
	<tr><td>'.__("User access level to plugin","mb-reservation").'</td><td><select  name="res_security_plugin" id="settings">';
	reset($role_list);
while (list ($key, $val)=each($role_list)){
if ($val==get_option("res_security_plugin")) $adder="selected" ; else $adder="";
	$content .= '<option '.$adder.' value="'.$val.'">'.translate_user_role($key);
}
	
	$content .= '</select></td></tr>
	<tr><td>'.__("User access level to settings","mb-reservation").'</td><td><select  name="res_security_settings" id="settings">';
	reset($role_list);
while (list ($key, $val)=each($role_list)){
if ($val==get_option("res_security_settings")) $adder="selected" ; else $adder="";
	$content .= '<option '.$adder.' value="'.$val.'">'.translate_user_role($key);
}
	$content .= '</select></td></tr></table>';
	
	$content .= '<h3>'.__("Deactivation settings","mb-reservation").'</h3>
	<table class="widefat page fixed" style="width:650px">
	<thead>
	<tr >
	<th >'.__("Parameter","mb-reservation").'</th>
	<th>'.__("Value","mb-reservation").'</th>
	</tr>
	</thead>
	<tr><td>'.__("Full uninstall (all data will be removed)","mb-reservation").'</td><td><input type="checkbox"  name="res_full_uninstall"'; 
	if(get_option("res_full_uninstall"))  $content.=' checked ';
	$content.='" onclick="if (this.checked == true) {return confirm(\''.__("Are you sure ? (all data will be removed)","mb-reservation").'\')}" /></td></tr></table>';
	
	
	
	$content .= '<h3>'.__("Other settings","mb-reservation").'</h3>
	<table class="widefat page fixed" style="width:650px">
	<thead>
	<tr >
	<th >'.__("Parameter","mb-reservation").'</th>
	<th>'.__("Value","mb-reservation").'</th>
	</tr>
	</thead>';
	/*
	//per_day & per_night calculate
	$content .='<tr><td>
	'.__("How to calculate employment resources","mb-reservation").'</td><td><select  name="res_calc" id="settings">';
	$calc_list= array (__("per day","mb-reservation")=>1,__("per night","mb-reservation")=>2);
	reset($calc_list);
while (list ($key, $val)=each($calc_list)){
if ($val==get_option("res_calc")) $adder="selected" ; else $adder="";
	$content .= '<option '.$adder.' value="'.$val.'">'.$key;
}
	$content .= '</select>
	</td></tr>';
	*/
	$content .='<tr><td>'.__("Terms of booking","mb-reservation").'</td><td><textarea   name="res_terms" rows="10" cols="40">'
	.get_option("res_terms") ."</textarea ></td></tr></table>";
	
	
	if ($GLOBALS["version"] == "pro")	$content .= pro_settings();
	
	$content .= '<div class="tablenav">

	<div class="alignleft actions">
	<input type="hidden" value="1" name="change_settings" >
	<input type="submit" value="'.__("Save","mb-reservation").'" name="doaction2" id="doaction2" class="button-primary" />
	</div>

	<br class="clear" />
	</div>
	</form>';
	echo $content;
}

function adm_pro()
{
	echo "adm_pro";
}



function treesort ()
{
	global $wpdb;
	$qry ="select  resourceid, parent , name, price, price1, price2, calcType, capacity, available, days, offline, remarks from ".$wpdb->prefix . "res_resources order by resourceid";
	$arr=$wpdb->get_results($wpdb->prepare($qry), ARRAY_N );

	for ($i=0;$i<count($arr)-1;$i++) {
		$flag=$i;
		for ($j=$i+1;$j<count($arr);$j++) {
			if ($arr[$j][1]==$arr[$i][0]) 
			{
				array_splice($arr,$flag+1,0,array(($arr[$j])));
				unset($arr[$j+1]);
				$arr = array_values($arr);			
				$flag=$flag+1;
			} 
			
		}
		
	}


	$map=array();		
	$level=array();		
	for ($k=0;$k<count($arr);$k++)	
	{	
		
		if ($arr[$k][1]==0) array_push($map,0);
		$z=1;
		foreach($arr as $key) {
			if($arr[$k][1]==$key[0]) {	array_push($map,$z);			}
			$z++;
		}
		
	}	
	
	
	
	$treet="";
	for ($k=0;$k<count($map);$k++)	
	{	
		array_push($level,treelevel($map,$k,0));
		if ($map[$k]==0 and array_search(($k+1),$map) ) { 
			$treet[] = $k+1;
		}
	}	
	
	if (count($treet)>1)	$treet = implode ("|", $treet); else $treet=$treet[0];
	
	return array($arr, $map, $treet, $level);
}

function treelevel ($map,$k,$level)

{
	if ($map[$k]==0)  { return $level;} else  $level=treelevel($map,$map[$k]-1,$level+1);
	return $level;
}


function treeselect ($results,$level,$interf)
{
	$content="<select name=\"resource\" id=\"sel1\">
	<option value=\"0\">All</option>
	";
	
	foreach($results as $key=>$value) {
		if($results[$key][4]!=0 && $interf!=3) continue;
		if($level[$key]!=0 && $interf==4) continue;
		if($level[$key]!=1 && $interf==42) continue;
		$adder="";
		for($i=1;$i<=$level[$key];$i++)	$adder.="-";		
		$content.="<option value=".$results[$key][0]."> $adder".$results[$key][2]."</option>";
	}
	$content.="</select>";
	return $content;
}

function charr ($plarr1, $plarr) {
global $wpdb;
$charr=array();
		
		// find all childs and save it to $charr
		$qry ="select  resourceid, parent , name, price, capacity from ".$wpdb->prefix . "res_resources order by resourceid";
		$results=$wpdb->get_results($wpdb->prepare($qry), ARRAY_N );
		list($results,$map)=treesort($results);	
		
		foreach ($plarr as $plarrv ) {
			$flag=0;
			foreach ($map as $key=>$value) {
				if ($results[$key][0]==$plarrv || $flag>0) { 
					if ($flag==0) {$fmap=$value; $flag +=1;}
					if ($fmap==$value && $flag>1 || $fmap>$value) break;  else $flag += 1;
					// check repated values in array, if value uniqal add it to array		
					if(!in_array($results[$key][0],$charr))			array_push($charr,$results[$key][0]);
				}
			}
		}
		$charr=( implode(",",$charr))	 ;
		$charr=array($plarr1,$charr);
		return $charr;
}

function adm_resources ()
{
	global $wpdb;
	
	$content="";
	
	
	list($results,$map)=treesort();
	//print_r($results);
	
	if ($_REQUEST['action']=="add") {
		if (empty($_REQUEST['price'])) $_REQUEST['price']=0;
		if (empty($_REQUEST['price1'])) $_REQUEST['price1']=0;
		if (empty($_REQUEST['price2'])) $_REQUEST['price2']=0;
		if (empty($_REQUEST['capacity'])) $_REQUEST['capacity']=1;
		if (empty($_REQUEST['available'])) $_REQUEST['available']='01013112';
		if (empty($_REQUEST['days'])) $_REQUEST['days']='MTWTFSS';
		if (empty($_REQUEST['offline'])) $_REQUEST['days']='0';
		if (empty($_REQUEST['remarks'])) $_REQUEST['days']='';
		
		
		$qry ="insert into ".$wpdb->prefix . "res_resources"." (parent, name,  price, price1, price2, capacity, available, days, offline, remarks) values (".intval($_REQUEST['idradd']).",'".$_REQUEST['name']."','".$_REQUEST['price']."','".$_REQUEST['price1']."','".$_REQUEST['price2']."','".$_REQUEST['capacity']."','".$_REQUEST['available']."','".$_REQUEST['days']."','".$_REQUEST['offline']."','".$_REQUEST['remarks']."')";
		
		$result = $wpdb->query( $wpdb->prepare($qry) );
		if (!$result) {
				die(mysql_error());
		}
		else {
			;
		}
		$qry = "update ".$wpdb->prefix . "res_resources set price=NULL, capacity=NULL where resourceid='".$_REQUEST['idradd']."'";
		$wpdb->query($wpdb->prepare($qry) );
		foreach($map as $key=>$value) {
			if ($results[$value-1][0]>0) {
				$qry = "update ".$wpdb->prefix . "res_resources set price=NULL, capacity=NULL where resourceid='".$results[$value-1][0]."'";
				
				$wpdb->query($wpdb->prepare($qry) );
			}
		}
		$content .= "<div class=alert>".__("Record added","mb-reservation")."</div>"		 ;
	}

	if ($_REQUEST['action']=="-1" ) {
		$content .= "<div class=alert>".__("You have not selected any of the operations","mb-reservation")."</div>"		 ;
	}

	if ($_REQUEST['action']=="edit" and count($_REQUEST['post'])>0) {

		foreach ($_REQUEST['post'] as $key=>$value) {
			echo $qry ="update ".$wpdb->prefix . "res_resources"." set name= '".$_REQUEST['name'][$key]
			."' ,  price = '".$_REQUEST['price'][$key]."' ,  "
			."' ,  price1 = '".$_REQUEST['price1'][$key]."' ,  "
			."' ,  price2 = '".$_REQUEST['price2'][$key]."' ,  "
			."' ,  calcType = '".$_REQUEST['calcType'][$key]."' ,  "
			."' ,  available = '".$_REQUEST['available'][$key]."' ,  "
			."' ,  days = '".$_REQUEST['days'][$key]."' ,  "
			."' ,  offline = '".$_REQUEST['offline'][$key]."' ,  "
			."' ,  remarks = '".str_replace("'","&quote;",$_REQUEST['remarks'][$key])."' ,  "
			." capacity = '".$_REQUEST['capacity'][$key]."'  where resourceid='".$value."' ";
			$wpdb->show_errors();
			$wpdb->query($wpdb->prepare($qry) );
		}
		$content .=  "<div class=alert>".__("Selected records changed","mb-reservation")."</div>"		 ;
	}

	if ($_REQUEST['action']=="delete") {
		$flag=0;
		foreach ($map as $key=>$value) {
			if ($results[$key][0]==$_REQUEST['idrdel'] || $flag>0) { 
				if ($flag==0) {$fmap=$value; $flag +=1;}
				if ($fmap==$value && $flag>1 || $fmap>$value) break; else $flag += 1;
				$qry ="delete from ".$wpdb->prefix . "res_resources"."  where resourceid='".$results[$key][0]."' ";
				$wpdb->query($wpdb->prepare($qry) );
			}
		}
		$content .= "<div class=alert>".__("Selected records deleted","mb-reservation")."</div>"		 ;
	}

		
	list($results,$map)=treesort();	
	
	$content .= '
	<h2>'.__("Resources","mb-reservation").'</h2>	';

	//wp_enqueue_script('jquery-form');
	
	
require_once(RES_PLUGIN_PATH."/js/jQTreeTable/jqtreetable.php");

	
	 
$options = '{openImg: "'.RES_PLUGIN_URL.'/js/jQTreeTable/images/fop.png", shutImg: "'.RES_PLUGIN_URL.'/js/jQTreeTable/images/fcl.png", leafImg: "'.RES_PLUGIN_URL.'/js/jQTreeTable/images/item.png", lastOpenImg: "'.RES_PLUGIN_URL.'/js/jQTreeTable/images/fop.png", lastShutImg: "'.RES_PLUGIN_URL.'/js/jQTreeTable/images/fcl.png", lastLeafImg: "'.RES_PLUGIN_URL.'/js/jQTreeTable/images/item.png", vertLineImg: "'.RES_PLUGIN_URL.'/js/jQTreeTable/images/blank.gif", blankImg: "'.RES_PLUGIN_URL.'/js/jQTreeTable/images/blank.gif", collapse: false, column: 1, striped: true, highlight: true, state:true}';
//$tbodyid = "treet1";



//$content.= $_REQUEST['expand'].$_REQUEST['asd_x'];


//echo $content;

   $head = array(__("ID","mb-reservation")."&nbsp;&nbsp;&nbsp;", __("Name","mb-reservation"),
   __("Price","mb-reservation"),    __("Price 1","mb-reservation"),    __("Price 2","mb-reservation"), __("Calculation","mb-reservation"),
   __("Capacity","mb-reservation") , __("Available","mb-reservation"),__("Weekdays","mb-reservation"),__("offline","mb-reservation"), __("Operations","mb-reservation"));
    
    for ($i=0; $i<count($head); $i++){
    	$headrow .= '<th >'.$head[$i].'</th>';
    }
    $headrow ='<tr>'.$headrow.'</tr>';
	
$topform = '<div class="res_acthead"><form action="" id="expand"  method="POST"><input type="hidden" name="page" value="resources"><div ><a href="#" id="additem'.$fld[0].'" ><img src="'.RES_PLUGIN_URL.'/js/jQTreeTable/images/addm.png" title="Add resource in root of tree" alt="Add resource in root of tree" ></a><input type=image src="'.RES_PLUGIN_URL.'/js/jQTreeTable/images/expand.png" name="expand" title="Expand" alt="Expand" ><input type=image src="'.RES_PLUGIN_URL.'/js/jQTreeTable/images/collapse.png" title="Collapse" alt="Collapse" name="collapse"></div></form></div>';
$topform =	 '<tr><th colspan="'.($i-1).'"></th><th  style="text-align:left">'.$topform.'</th></tr>';

	$x=1; 	//$x number of row in resource table (now deprecated)
	foreach ($results as $fld) {

      if (empty($fld[6]) || $fld[6] == '0') {
		$stdchecked = " checked='checked' ";
		$splchecked = "";
	  }
	  else {
		$splchecked = " checked='checked' ";
		$stdchecked = "";
	  }
      $tabstr .= '<tr><td><form id="treeform'.$fld[0].'" action="" method="post"><input type="hidden" name="resourceid" value="'.$fld[0].'">'.($fld[0]).'</td><td>&nbsp; <input name="name" id="name'.$fld[0].'" value="'.$fld[2].'"></td>
	  <td><input name="price" class="res_price" id="price_'.$fld[0].'" value="'.$fld[3].'"></td>
	  <td><input name="price1" class="res_price" id="price1_'.$fld[0].'" value="'.$fld[4].'"></td>
	  <td><input name="price2" class="res_price" id="price2_'.$fld[0].'" value="'.$fld[5].'"></td>
	  <td><input type="radio" name="calcType" class="res_price_radio" id="calcType_'.$fld[0].'" value="0" '.$stdchecked.'>Standard
	  <input type="radio" name="calcType" class="res_price_radio" id="calcType_'.$fld[0].'" value="1" '.$splchecked.'>Days
	  </td>
	  
	  <td><input name="capacity" class="res_capacity" id="capacity_'.$fld[0].'" value="'.$fld[7].'"></td>
	  <td><input name="available" class="res_capacity" id="available_'.$fld[0].'" value="'.$fld[8].'"></td>
	  <td><input name="days" class="res_capacity" id="days_'.$fld[0].'" value="'.$fld[9].'"></td>
	  <td><input name="offline" class="res_capacity" id="offline_'.$fld[0].'" value="'.$fld[10].'"></td>
	  <td><div><a href="#" id="delitem'.$fld[0].'" ><img src="'.RES_PLUGIN_URL.'/js/jQTreeTable/images/del.png"></a></div><div id="soutput'.$fld[0].'"><input type="image" src="'.RES_PLUGIN_URL.'/js/jQTreeTable/images/save.png" class="submit_form"></div><div id="toutput'.$fld[0].'"><img src="'.RES_PLUGIN_URL.'/js/jQTreeTable/images/tick.png" ></div><div id="loutput'.$fld[0].'"><img src="'.RES_PLUGIN_URL.'/js/jQTreeTable/images/load.gif"></div><div><a href="#" id="additem'.$fld[0].'" ><img src="'.RES_PLUGIN_URL.'/js/jQTreeTable/images/add.png"></a></div></td></tr>
	  <tr><td style="text-align: left;" colspan="10"><strong>Remarks:&nbsp;</strong><input style="width : 500px;" name="remarks"  id="remarks_'.$fld[0].'" value="'.$fld[11].'"></form></td></tr>
	  '."\n";
$outarr.="$fld[0],";
	  $x++;
	  
    }
$tbodyid = "treet";	
$str=	('<table class="tablemain"><thead>'.$topform.$headrow."</thead>\n<tfoot>".$headrow."</tfoot>\n".'<tbody id="'.$tbodyid.'">'.$tabstr."</tbody></table>\n");
$outarr="[".substr($outarr,0,-1)."]";

 $content .= <<<EOF
    <p><strong>Available: </strong>The period when tour is available. e.g. Whole year: 01013112 and only june: 01063006
	<br><strong>Offline: </strong>If set to '1' the will not be shown on the reservation page. It is not advisable to delete a resource.
	<br><strong>Remarks: </strong>A remark will be put in the reservation page.
	<br><strong>Weekdays:  </strong>Tour only available on weekdays. e.g. Every day: MTWTFSS and only weekend: XXXXFSS</p>
	

	<script type="text/javascript">
	jQuery(function() {
	var  outarr=$outarr;

	
for(i=0;i<=
EOF
.($x-2).
<<<EOF
;i++) {
	jQuery("#soutput"+outarr[i]).hide();	
	jQuery("#loutput"+outarr[i]).hide();	
	}
var name = jQuery("#name"),idradd=jQuery("#idradd"),idrdel=jQuery("#idrdel")
allFields = jQuery([]).add(name).add(idradd).add(idrdel);	
		jQuery("#dialogadd").dialog({
			bgiframe: true,
			height: 240,
			modal: true,
			autoOpen: false,
			buttons: {
				'Cancel': function() {
				allFields.val('').removeClass('ui-state-error');
					jQuery(this).dialog('close');
				},
				'Save': function() {
				document.formadd.submit();
				allFields.val('').removeClass('ui-state-error');
				
					jQuery(this).dialog('close');
				}
			},
			open: function() {
				idradd.val(Id).removeClass('ui-state-error');
			}
			,
			close: function() {
				allFields.val('').removeClass('ui-state-error');
			}
		});
		jQuery("#dialogdel").dialog({
			bgiframe: true,
			height: 240,
			modal: true,
			autoOpen: false,
			buttons: {
				'No': function() {
				 allFields.val('').removeClass('ui-state-error');
					jQuery(this).dialog('close');
				},
				'Yes': function() {
				document.formdel.submit();
				allFields.val('').removeClass('ui-state-error');
				
					jQuery(this).dialog('close');
				}
			},
			open: function() {
				idrdel.val(Id).removeClass('ui-state-error');
			}
			
		});


jQuery('a').click(function() {
var element = jQuery(this);
Id = element.attr("id");

if (Id.replace(/additem.*/,'additem')=="additem") {
Id=Id.replace(/^[^\d]+/,'');
jQuery('#dialogadd').dialog('open');

}
if (Id.replace(/delitem.*/,'delitem')=="delitem") {
Id=Id.replace(/^[^\d]+/,'');
jQuery('#dialogdel').dialog('open');
}
			})	;		
	});
	
	</script>



<div id="dialogadd" title="
EOF
.__("Adding resource","mb-reservation").
<<<EOF
">
	<p>
EOF
.__("Please input a resource name","mb-reservation").
<<<EOF
</p>
	<form action="" method="post"  name="formadd"><input name="name" id="name" size="33" value=""><input type="hidden" name="idradd" id="idradd" value=""><input type="hidden" name="action"  value="add"></form>
	
</div>

<div id="dialogdel" title="
EOF
.__("Deleting resource","mb-reservation").
<<<EOF
">
	<p>
EOF
.__("Are you sure you want to delete this entry, will also remove all children resources","mb-reservation").
<<<EOF
</p>
	<form action="" method="post"  name="formdel"><input type="hidden" name="idrdel" id="idrdel" value=""><input type="hidden" name="action"  value="delete"></form>
	
</div>
EOF;

$map=implode(", ", $map);


$body = $str;
$jq = new jQTreeTable();

   
//$tbodyid="treet3";
//$map="0, 0, 2, 3, 4, 4, 6, 4, 2, 9, 10, 0";
$vars = $jq->init($map,$options,$tbodyid);
  $content .= '<script type="text/javascript">
{'.$vars[0].'}
    </script>
'.$body;

$content .= <<<EOF
<script type="text/javascript">
jQuery('input').change(function() {
var element = jQuery(this);
Id = element.attr("id");

Id=Id.split('_')[1];
//Id=Id.replace(/price/,'');
//alert(Id);
jQuery("#toutput"+Id).hide();
jQuery("#soutput"+Id).show();
});


jQuery('form').submit(function() { 
  var element = jQuery(this);
  Idelement = element.attr("id");
  Id=Idelement.replace(/^[^\d]+/,'');
   var options = { 
    target: "#loutput"+Id,
    beforeSubmit: showRequest, 
    success: showResponse, 
    timeout: 3000 
  };
  
    jQuery(this).ajaxSubmit(options); 
	if (Idelement=="addresource" || Idelement=="expand" ) return true; else
	  return false;
  }); 


function showRequest(formData, jqForm, options) { 


jQuery("#loutput"+Id).show();
jQuery("#soutput"+Id).hide();	
    var queryString = jQuery.param(formData); 
    return true; 
} 

function showResponse(responseText, statusText)  { 
jQuery("#loutput"+Id).hide();	 
jQuery("#toutput"+Id).show();	

}

</script>
EOF;


	echo $content;
}

function adm_orders()
{
	global $wpdb;

	$content="";
	
	if ($_REQUEST['add_order']) {
		$qry ="insert into ".$wpdb->prefix . "res_resources"." (name, price) values ('".$_REQUEST['name']."','".$_REQUEST['price']."')";
		$wpdb->query($wpdb->prepare($qry) );
		$content .= "<div class=alert>".__("Record added","mb-reservation")."</div>"		 ;
	}


	if ($_REQUEST['action']=="-1" ) {
		$content .= "<div class=alert>".__("You have not selected any of the operations","mb-reservation")."</div>"		 ;
	}

	if ($_REQUEST['action']=="edit" and count($_REQUEST['post'])>0) {
		$rowsupdated = 0;
		foreach ($_REQUEST['post'] as $key=>$value) {
		
		 $kolday=(strtotime($_REQUEST['dateend'][$key])-strtotime($_REQUEST['datebegin'][$key]))/(24*60*60)+1;
		  
		  
		 $qry="select  kol,  b.price  from  ".$wpdb->prefix . "res_orders_content b
			INNER JOIN ".$wpdb->prefix . "res_resources a ON (a.resourceid = b.resourceid) where orderid=".$value;

		$results=$wpdb->get_results($wpdb->prepare($qry), ARRAY_N );

		if (count($results)>0) {
			$i=1;$kolm=0;$summ=0;
				foreach ($results as $fld) {
				$summ+=$fld[0]*$fld[1];
				$i++;
				}
			  
		}
		
			$wpdb->show_errors();
			$qry ="update ".$wpdb->prefix . "res_orders"." set price = '".$summ*$kolday."' ,  datebegin = '".$_REQUEST['datebegin'][$key]."' ,  dateend = '".$_REQUEST['dateend'][$key]."',  comments = '".$_REQUEST['comments'][$key]."',  payed = '".intval($_REQUEST['payed'][$key])."' where orderid=".intval($value)." ";
			$wpdb->query($wpdb->prepare($qry) );
			$rowsupdated++;
		}
		$content .=  "<div class=alert>".__("Selected records changed","mb-reservation")." ($rowsupdated)</div>"		 ;
	}

	if ($_REQUEST['action']=="delete" and count($_REQUEST['post'])>0) {
		foreach ($_REQUEST['post'] as $key=>$value) {
			$qry ="delete from ".$wpdb->prefix . "res_orders"."  where orderid='".$value."' ";
			$wpdb->query($wpdb->prepare($qry) );
			$qry ="delete from ".$wpdb->prefix . "res_orders_content"."  where orderid='".$value."' ";
			$wpdb->query($wpdb->prepare($qry) );


		}
		$content .=  "<div class=alert>".__("Selected records deleted","mb-reservation")."</div>"		 ;
	}





	$content .= '
	<h2>'.__("Orders","mb-reservation").'</h2>

	<form id="form2" name="form2" method="post" action="">
	<table class="widefat page fixed" style="">
	<thead>
	<tr>
	<th scope="col" id="cb" class="manage-column column-cb check-column" style=""><input type="checkbox" /></th>
	<th width=20>№</th>
	<th width=40>Tour/Bike</th>
	<th>'.__("Client","mb-reservation").'</th>
	<th>'.__("Cost","mb-reservation").'</th>
	<th>'.__("Start date","mb-reservation").'</th>
	<th>'.__("End date","mb-reservation").'</th>
	<th>'.__("Comment","mb-reservation").'</th>
	<th>'.__("Payment System","mb-reservation").'</th>
	<th width=30>'.__("Paid","mb-reservation").'</th>
	</tr>
	</thead>
';

if (isset($_REQUEST['ordert'])) $adder=" and ".$wpdb->prefix ."res_orders.orderid = \"".$_REQUEST['ordert']."\" ";
if (isset($_REQUEST['orderd'])) $adder=" and \"".$_REQUEST['orderd']."\" between datebegin AND dateend";

	
$qry="select ".$wpdb->prefix ."res_orders.orderid, $extrafld
			userid , ".$wpdb->prefix ."res_orders.price, 
			datebegin, dateend, comments , paysys, name,  payed,
			max((if(meta_key = 'first_name', meta_value, NULL ))) first_name,
			max((if(meta_key = 'last_name', meta_value, NULL ))) last_name,
			max((if(meta_key = 'nickname', meta_value, NULL ))) nickname 
from
".$wpdb->prefix . "usermeta ".
" inner join ".$wpdb->prefix . "res_orders on (userid = user_id) ".
" inner join ".$wpdb->prefix . "res_orders_content as roc on (roc.orderid = ".$wpdb->prefix . "res_orders.orderid) ".
" left join ".$wpdb->prefix . "res_paysys on (paysys = paysysid)
where meta_key in ('first_name','last_name','nickname' ) $adder 
group by orderid, userid , price , datebegin, dateend, comments , paysys, payed,name
";


	$results=$wpdb->get_results($wpdb->prepare($qry), ARRAY_A);

	$pageadr=$_REQUEST['page'];
	if (count($results)>0) {
		$i=1;
		foreach ($results as $fld) {

			extract($fld);

			$payedadder="";
			if ($payed > 0) $payedadder="checked";

			$content .= <<<EOF
			<tr>
			<th scope="row" class="check-column"><input type="checkbox" name="post[$i]" value="$orderid" /></th>
			<td><a href="?page=$pageadr&subpage=suborder&userid=$userid&last_name=$last_name&first_name=$first_name&price=$price&datebegin=$datebegin&dateend=$dateend&orderid=$orderid">$orderid</a></td>
			<td>$resourceid</td>
			<td><input type="hidden" name="orderid[$i]" value="$orderid" />
			<input type="hidden" name="userid[$i]" value="$userid" />
			<a href="user-edit.php?user_id=$userid" title="$nickname">$last_name</a>
			<a href="?page=$pageadr&subpage=mail&userid=$userid&last_name=$last_name&first_name=$first_name&price=$price&datebegin=$datebegin&dateend=$dateend&orderid=$orderid" title="
EOF
.__("Send email to customer","mb-reservation").
<<<EOF
			" 
EOF;
if ($GLOBALS["version"]	!= "pro")  $content.="onclick=\"alert('".__("This function working only in PRO version","mb-reservation")."'); return false;\"";
$content .= <<<EOF
			 ><img src="images/comment-grey-bubble.png"></a> 


			</td>
			<td><input type="hidden" name="price[$i]" value="$price"  />$price</td>
			<td><input type="text" name="datebegin[$i]" value="$datebegin"  /></td>
			<td><input type="text" name="dateend[$i]" value="$dateend"  /></td>
			<td><textarea rows=4 cols=30 type="text" name="comments[$i]" >$comments</textarea></td>
			<td>$name</td>
			<td><input type="checkbox" $payedadder name="payed[$i]" value="1"  /></td>

			</tr>
EOF;
			$i++;
		}
	}


	$content .= '

	</table>

	<div class="tablenav">

	<div class="alignleft actions">
	<select name="action">
	<option value="-1" selected="selected">'.__("Bulk Actions","mb-reservation").'</option>
	<option value="edit">'.__("Change","mb-reservation").'</option>
	<option value="delete">'.__("Delete","mb-reservation").'</option>
	</select>
	<input type="submit" value="'.__("Apply","mb-reservation").'" name="doaction" id="doaction" class="button-primary" />
	</div>

	<br class="clear" />
	</div>	
	
	</form>
';
	echo $content;
}


function adm_suborder()
{
	global $wpdb;
		
	$content="";
	$kolday=(strtotime($_REQUEST['dateend'])-strtotime($_REQUEST['datebegin']))/(24*60*60)+1;
	
	
	if ($_REQUEST['add_sub_order']) {
		if ($_REQUEST['resourceid'] == -1 or $_REQUEST['kol']== "" or $_REQUEST['price']=="") { $content .= "<div class=alert>".__("To add a record, you must specify all the parameters. Record was not added.","mb-reservation")."</div>"; } else {
			$qry ="insert into ".$wpdb->prefix . "res_orders_content"." (orderid, resourceid, kol, price) values ('".$_REQUEST['orderid']."','".$_REQUEST['resourceid']."','".$_REQUEST['kol']."','".$_REQUEST['price']."')";
			$wpdb->query($wpdb->prepare($qry) );
			$qry ="update ".$wpdb->prefix . "res_orders"." set price = price + '".$_REQUEST['kol']*$_REQUEST['price']."' where orderid='".$_REQUEST['orderid']."' ";
			$wpdb->query($wpdb->prepare($qry) );
			$content .= "<div class=alert>".__("Record added","mb-reservation")."</div>"		 ;}
	}

	if ($_REQUEST['action']=="-1" ) {
		$content .= "<div class=alert>".__("You have not selected any of the operations","mb-reservation")."</div>"		 ;
	}


	if ($_REQUEST['action']=="edit" and count($_REQUEST['post'])>0) {
		$summ=0;$summold=0;
		
		foreach ($_REQUEST['post'] as $key=>$value) {
			$qry ="update ".$wpdb->prefix . "res_orders_content"." set resourceid= '".$_REQUEST['resourceid'][$key]."' ,  price = '".$_REQUEST['price'][$key]."' ,  kol = '".$_REQUEST['kol'][$key]."' where id='".$value."' ";
			$wpdb->query($wpdb->prepare($qry) );
			$summ+=$_REQUEST['kol'][$key]*$_REQUEST['price'][$key];
			//$summold+=$_REQUEST['oldkol'][$key]*$_REQUEST['oldprice'][$key];
		}
		$qry ="update ".$wpdb->prefix . "res_orders"." set price =   ".($summ*$kolday)." where orderid='".$_REQUEST['orderid']."' ";	
		
		$wpdb->query($wpdb->prepare($qry) );

		
		$content .=  "<div class=alert>".__("Selected records changed","mb-reservation")."</div>"		 ;
	}

	if ($_REQUEST['action']=="delete" and count($_REQUEST['post'])>0) {
		$summold=0;
		foreach ($_REQUEST['post'] as $key=>$value) {
			$qry ="delete from ".$wpdb->prefix . "res_orders_content"."  where id='".$value."' ";
			$wpdb->query($wpdb->prepare($qry) );
			$summold+=$_REQUEST['oldkol'][$key]*$_REQUEST['oldprice'][$key];
		}
		$qry ="update ".$wpdb->prefix . "res_orders"." set price = price - '".$summold."' where orderid='".$_REQUEST['orderid'][$key]."' ";
		$wpdb->query($wpdb->prepare($qry) );	
		$content .=  "<div class=alert>".__("Selected records deleted","mb-reservation")."</div>"		 ;
	}


	$qryso= "select resourceid, name  from  ".$wpdb->prefix . "res_resources ";  	
	$resso=$wpdb->get_results($wpdb->prepare($qryso), ARRAY_N );

	$select=createselectoption ($fld[2], $resso, "resourceid",__("Select","mb-reservation"))	;

	$content .= '
	<h2>'.__("Composition of order","mb-reservation").' #'.$_REQUEST['orderid'].'</h2>
	<h3>'.__("Adding record","mb-reservation").'</h3>
	<form id="form1" name="form1" method="post" action="">
	<table class="widefat page fixed" style="width:650px">
	<thead>
	<tr >
	<th>'.__("Resources","mb-reservation").'</th>
	<th >'.__("Reserved places","mb-reservation").'</th>
	<th>'.__("Price per place","mb-reservation").'</th>
	</tr>
	</thead>
	<tr>
	<td>'.$select.'<input type="hidden" name="orderid" value='.$_REQUEST['orderid'].' /></td>
	<td><input type="text" name="kol"  /></td>
	<td><input type="text" name="price"  /></td>
	


	</tr>
	</table>
	<div class="tablenav">

	<div class="alignleft actions">
	<input type="hidden" value="1" name="add_sub_order" >
	<input type="submit" value="'.__("Add","mb-reservation").'" name="doaction" id="doaction" class="button-primary" />

	</div>

	<br class="clear" />
	</div>
	</form>

	<h3>'.__("Changing records","mb-reservation").'</h3>
	<form id="form2" name="form2" method="post" action="">
	<table class="widefat page fixed" style="width:650px">
	<thead>
	<tr>
	<th scope="col" id="cb" class="manage-column column-cb check-column" style=""><input type="checkbox" /></th>
	<th>'.__("Resources","mb-reservation").'</th>
	<th >'.__("Reserved places","mb-reservation").'</th>
	<th>'.__("Price per place","mb-reservation").'</th>
	
	</tr>
	</thead>
';

	$qry="select id, orderid, b.resourceid, name ,  kol,  b.price  from  ".$wpdb->prefix . "res_orders_content b
INNER JOIN ".$wpdb->prefix . "res_resources a ON (a.resourceid = b.resourceid) where orderid=".$_REQUEST['orderid'];
	$results=$wpdb->get_results($wpdb->prepare($qry), ARRAY_N );

	if (count($results)>0) {
		$i=1;$kolm=0;$summ=0;
		foreach ($results as $fld) {
			$select=createselectoption ($fld[2], $resso, "resourceid[$i]","")	;
			$content .= <<<EOF
			<tr>    <th scope="row" class="check-column"><input type="checkbox" name="post[$i]" value="$fld[0]" /></th>
			<td><input type="hidden" name="id[$i]" value="$fld[0]" /><input type="hidden" name="orderid[$i]" value="$fld[1]" />$select </td>
			<td><input type="hidden" name="oldkol[$i]" value="$fld[4]"  /><input type="text" name="kol[$i]" value="$fld[4]"  /></td>
			<td><input type="hidden" name="oldprice[$i]" value="$fld[5]"  /><input type="text" name="price[$i]" value="$fld[5]"  /></td>
			
			</tr>
EOF;
			$kolm+=$fld[4];
			$summ+=$fld[4]*$fld[5];
			$i++;
		}
	}

	$content .= '
	<tfoot>
	<tr>
	<th > </th>
	<th>'.__("Total","mb-reservation").'</th>
	<th > '.__("places","mb-reservation").': '.$kolm.'</th>
	<th>'.__("price","mb-reservation").': '.$summ.'</th>
	
	</tr>
	</tfoot>
	</table>
	<div class="tablenav">

	<div class="alignleft actions">
	<select name="action">
	<option value="-1" selected="selected">'.__("Bulk Actions","mb-reservation").'</option>
	<option value="edit">'.__("Change","mb-reservation").'</option>
	<option value="delete">'.__("Delete","mb-reservation").'</option>
	</select>
	<input type="hidden" name="orderid" value='.$_REQUEST['orderid'].' />
	<input type="submit" value="'.__("Apply","mb-reservation").'" name="doaction" id="doaction" class="button-primary" />
	</div>	
	
	<br class="clear" />
	</div>

	</form>
';

	echo $content;
}

function adm_ordertable()
{
	global $wpdb;
	return 	pro_ordertable();
}	

function createselectoption ($id, $arr, $name, $first)
{
	$content="<select name=$name>";
	if (($first)!="") $content.="<option $adder value=\"-1\">$first</option>";
	foreach ($arr as $opt) {
		if ($opt[0]==$id) $adder="selected"; else $adder="";
		$content.="<option $adder value=\"$opt[0]\">$opt[1]</option>";
		
	}
	$content.=	"</select>";
	return $content;
}

function adm_makeorder0() {

	$today = date("Ymd");  $today2 = date("d.m.Y"); 
	
	$content = "";

	$content .= <<<EOF
<h2>
EOF
	.__("Reserve","mb-reservation").
	<<<EOF
	</h2>
	<p>
EOF
	.__("To see prices and availability, enter your arrival date and length of stay in the search form below.","mb-reservation").
	<<<EOF
	</p>
	
	<form name="res_order"  action="" method=post ><input type="hidden" name=page value="makeorder1" >
	
	<input size="10" id="datebegin2" name="datebegin" value="$today2" /> <button class="button-primary" id="f_btn2" onclick="return false;">...</button> 
	<select name=kolday class="koldays" >
EOF;
	$maxdays=get_option("res_days");
	for($i=1;$i<=$maxdays;$i++)
	{$content .= "<option value=\"$i\" >$i</option>";}	
	$content .= <<<EOF
	</select>
	


	
	
	
	
	<input  class="button-primary"  type="submit" value="
EOF
.__("Continue","mb-reservation").
<<<EOF
	"   ></form>

	<script type="text/javascript">//<![CDATA[

	var cal = Calendar.setup({
onSelect: function(cal) { cal.hide() }
		,
min:$today 

		
		
		
	});
	cal.manageFields("f_btn2", "datebegin2", "%d.%m.%Y");


	//]]></script>	
	
EOF;

	echo $content ;
};

function adm_makeorder1() {
	global $wpdb;
	$content = "";

	if ($_REQUEST['page']	== "makeorder1") {
		list($day, $month, $year) = sscanf($_REQUEST['datebegin'], "%02d.%02d.%04d"); 
		$datebegin=$year."-". $month."-". $day;
		$dateend=date("Y-m-d",strtotime($datebegin)+($_REQUEST['kolday']-1)*24*60*60);
	} else {
		
		$datebegin=$_REQUEST['datebegin'];
		$dateend=$_REQUEST['kolday'];
	}
	


	$content .= "<p>".__("Please select resources you want to book, and desired amount.","mb-reservation")."</p>";

	
	$qry="select a.resourceid, name ,   a.price ";
	
	$dt=strtotime ($datebegin);
	
	
	
	$bt = '<form name="res_order" id="res_order"  action="" method="post"> <table class="widefat page fixed" width=100%  > <thead>	<tr ><th style=\"width:200px\" > </th>';
	while (strtotime($dateend)>=$dt)  {
		$qdt=date("Y-m-d",$dt);
		$qdtout=date("d/m",$dt);

		$qry.=",    capacity - sum(if ( '$qdt' between datebegin and dateend , kol, 0)) `$qdt`  ";
		$bt .= "<th  >$qdtout</th>";
		$dt=$dt+24*60*60;
	}
	$bt .= '<th  >'.__("price","mb-reservation").'</th><th  >'.__("persons","mb-reservation").'</th></tr></thead>';
	$qry.="   from  
	".$wpdb->prefix . "res_orders  c
INNER JOIN  ".$wpdb->prefix . "res_orders_content   b 
ON (c.orderid =
	b.orderid)
	RIGHT JOIN  ".$wpdb->prefix . "res_resources   a ON (b.resourceid =
	a.resourceid)
	where capacity>0 
		group by name, a.resourceid, a.price ";

	

	$results=$wpdb->get_results($wpdb->prepare($qry), ARRAY_N );

	$rowflag=1;  $j=1;
	if (count($results)>0) {
		
		$content .= $bt;
		foreach ($results as $fld) {
			$content .= '<tr class="row'.$rowflag.'">';
			$k=1;
			$min=$fld[3];
			foreach ($fld as $fld2) {

				switch ($k) {
				case 1:   $resource=$fld2; break;
				case 2: $content .= "<td >".$fld2."</td>"; break;
				case 3: $price = $fld2;  break;
				default:  $content .= "<td >$fld2</td>"; if ($fld2<$min) $min=$fld2; break;
				}
				$k++;
			}
			
			$content .= '<td>'.$price.'</td><td><input type=hidden name=price['.$resource.'] value="'.$price.'"><input type=hidden name=resource_name['.$resource.'] value="'.$fld[1].'">';
			
			//if ($minorder<=$min ) 
			{ 
				$content .= '<select col=30  name=reskol['.$resource.']> <option value="0">'.__("select","mb-reservation").'</option>   ';
				if ($j>1) $javaif .= " && (obj.elements['reskol[$resource]'].value==0) "  ; else $javaif = " (obj.elements['reskol[$resource]'].value==0) ";
				for ($i=1;$i<=$min; $i++) {
					$content .= '<option value="'.$i.'">'.$i.'</option>';			
				}
				$content .= '</select>' ; 
			} 
			
			$content .= '</td></tr>';			
			$j++;
			$rowflag = $rowflag*(-1);

		}

		$content .= '<tr><td colspan='.(count($fld)-2).'></td><td align=right><br><input onclick="return checkForm();"  type="submit" value="'.__("Book now","mb-reservation").'"   ></td></tr></table><div align=right><input type="hidden" name=page value="makeorder2" ><input type="hidden" name="datebegin" value="'.$datebegin.'" ><input type="hidden" name="dateend" value="'.$dateend.'" ><input type="hidden" name="kolday" value="'.$_REQUEST['kolday'].'" > <BR></div></form>';			

		$content .= <<<EOF
		<script language="javascript">
		<!--
		function checkForm() {
			var err = "";
			var obj = document.forms['res_order'];
			
			
			if ( true $javaif ) {
				err += "  ";
			}
			
			

			if (err!="") {
				err = "
EOF
.__("You must select at least one of the numbers","mb-reservation").
<<<EOF
				" + err;
				alert(err); return false;
			} else {

				return true;
			}
		}
		-->
		</script> 
EOF;
		
		
	}
	else $content .= "<p>".__("No information about free resources","mb-reservation")."</p>";
	echo $content ;
}

function adm_makeorder2 () {
	global $wpdb;
	$content = "";

	$reskol=$_REQUEST['reskol'];
	$price=$_REQUEST['price'];
	$kolday=$_REQUEST['kolday'];

	foreach ($_REQUEST['reskol'] as $key=>$value )
	{
		$sumprice+=$reskol[$key]*$price[$key]*$kolday;

	}

	$qry ="insert into ".$wpdb->prefix . "res_orders"." (userid, price, datebegin, dateend) values ('1','$sumprice','".$_REQUEST['datebegin']."','".$_REQUEST['dateend']."')";
	
	
	$wpdb->query($wpdb->prepare($qry) );	
	$orderid= mysql_insert_id();

	foreach ($_REQUEST['reskol'] as $key=>$value )
	{
		if ($reskol[$key]>0 ) {
			$qry ="insert into ".$wpdb->prefix . "res_orders_content"." (orderid, resourceid, kol, price) values ('$orderid','$key','$reskol[$key]','$price[$key]')";
			$wpdb->query($wpdb->prepare($qry) );	}
	}

	$content.=	"<p>".__("Created an order","mb-reservation")." №$orderid </p>";
	echo $content;
}

function adm_offers()
{
	global $wpdb;

	$content="";
	
	if ($_REQUEST['add_offers']) {
		if ($_REQUEST['resourceid'] == -1 or $_REQUEST['price']== "" or $_REQUEST['datebegin']=="" or $_REQUEST['dateend']=="" ) { $content .= "<div class=alert>".__("To add a record, you must specify all the parameters. Record was not added.","mb-reservation")."</div>"; } else {
			$qry ="insert into ".$wpdb->prefix . "res_offers"." (resourceid, price, datebegin, dateend , description) values ('".$_REQUEST['resourceid']."','".$_REQUEST['price']."','".$_REQUEST['datebegin']."','".$_REQUEST['dateend']."','".$_REQUEST['description']."')";
			$wpdb->query($wpdb->prepare($qry) );
			
			$content .= "<div class=alert>".__("Record added","mb-reservation")."</div>"		 ;}
	}

	if ($_REQUEST['action']=="-1" ) {
		$content .= "<div class=alert>".__("You have not selected any of the operations","mb-reservation")."</div>"		 ;
	}

	if ($_REQUEST['action']=="edit" and count($_REQUEST['post'])>0) {
		
		foreach ($_REQUEST['post'] as $key=>$value) {
			$qry ="update ".$wpdb->prefix . "res_offers"." set resourceid= '".$_REQUEST['resourceid'][$key]."' ,  price = '".$_REQUEST['price'][$key]."' ,  datebegin = '".$_REQUEST['datebegin'][$key]."',   dateend = '".$_REQUEST['dateend'][$key]."',  description = '".$_REQUEST['description'][$key]."' where id='".$value."' ";
			$wpdb->query($wpdb->prepare($qry) );
			
		}
		
		
		

		$content .=  "<div class=alert>".__("Selected records changed","mb-reservation")."</div>"		 ;
	}

	if ($_REQUEST['action']=="delete" and count($_REQUEST['post'])>0) {
		$summold=0;
		foreach ($_REQUEST['post'] as $key=>$value) {
			$qry ="delete from ".$wpdb->prefix . "res_offers"."  where id='".$value."' ";
			$wpdb->query($wpdb->prepare($qry) );
			
		}
		
		$content .=  "<div class=alert>".__("Selected records deleted","mb-reservation")."</div>"		 ;
	}


	$qryso= "select resourceid, name  from  ".$wpdb->prefix . "res_resources ";  	
	$resso=$wpdb->get_results($wpdb->prepare($qryso), ARRAY_N );

	$select=createselectoption ($fld[2], $resso, "resourceid",__("select","mb-reservation"))	;

	$content .= '
	<h2>'.__("Special offers","mb-reservation").'</h2>
	<h3>'.__("Adding record","mb-reservation").'</h3>
	<form id="form1" name="form1" method="post" action="">
	<table class="widefat page fixed" >
	<thead>
	<tr >
	<th>'.__("Resources","mb-reservation").'</th>
	<th>'.__("Price per place","mb-reservation").'</th>	
	<th>'.__("Start date","mb-reservation").'</th>	
	<th>'.__("End date","mb-reservation").'</th>	
	<th>'.__("Comment","mb-reservation").'</th>	
	</tr>
	</thead>
	<tr>
	<td>'.$select.'</td>
	<td><input type="text" name="price"  /></td>
	<td><input type="text" name="datebegin"  /></td>
	<td><input type="text" name="dateend"  /></td>
	<td><input type="text" name="description"  /></td>
	


	</tr>
	</table>
	<div class="tablenav">

	<div class="alignleft actions">
	<input type="hidden" value="1" name="add_offers" >
	<input type="submit" value="'.__("Add","mb-reservation").'" name="doaction" id="doaction" class="button-primary" />

	</div>

	<br class="clear" />
	</div>
	</form>

	<h3>'.__("Changing records","mb-reservation").'</h3>
	<form id="form2" name="form2" method="post" action="">
	<table class="widefat page fixed" >
	<thead>
	<tr>
	<th scope="col" id="cb" class="manage-column column-cb check-column" style=""><input type="checkbox" /></th>
	<th>'.__("Resources","mb-reservation").'</th>
	<th>'.__("Price per place","mb-reservation").'</th>	
	<th>'.__("Start date","mb-reservation").'</th>	
	<th>'.__("End date","mb-reservation").'</th>	
	<th>'.__("Comment","mb-reservation").'</th>	

	
	</tr>
	</thead>
';


	$qry="select id, b.resourceid,  b.price, datebegin, dateend,description  from  ".$wpdb->prefix . "res_offers b
INNER JOIN ".$wpdb->prefix . "res_resources a ON (a.resourceid = b.resourceid)" ;
	$results=$wpdb->get_results($wpdb->prepare($qry), ARRAY_N );

	

	
	
	

	if (count($results)>0) {
		$i=1;$kolm=0;$summ=0;
		foreach ($results as $fld) {
			$select=createselectoption ($fld[1], $resso, "resourceid[$i]","")	;
			$content .= <<<EOF
			<tr>    <th scope="row" class="check-column"><input type="checkbox" name="post[$i]" value="$fld[0]" /></th>
			<td><input type="hidden" name="id[$i]" value="$fld[0]" />$select </td>
			<td><input type="text" name="price[$i]" value="$fld[2]"  /></td>
			<td><input type="text" name="datebegin[$i]" value="$fld[3]"  /></td>
			<td><input type="text" name="dateend[$i]" value="$fld[4]"  /></td>
			<td><input type="text" name="description[$i]" value="$fld[5]"  /></td>
			
			</tr>
EOF;
			$kolm+=$fld[4];
			$summ+=$fld[4]*$fld[5];
			$i++;
		}
	}



	$content .= '
	</table>
	<div class="tablenav">

	<div class="alignleft actions">
	<select name="action">
	<option value="-1" selected="selected">'.__("Bulk Actions","mb-reservation").'</option>
	<option value="edit">'.__("Change","mb-reservation").'</option>
	<option value="delete">'.__("Delete","mb-reservation").'</option>
	</select>
	<input type="submit" value="'.__("Apply","mb-reservation").'" name="doaction" id="doaction" class="button-primary" />
	</div>	

	<br class="clear" />
	</div>

	</form>
';

	echo $content;
}


?>